AimSkyward
AimSkyward

Reputation: 145

Text file Trauma in Python

I know this is basic but it has been nagging me for a while now. I am new to Python coding so please have patience.

I am using a Python script to read from MySQL and relay this information to a .txt file using the first line of an existing text file as the name of the new one. All of the MySQL stuff is working fine but I have a problem writing to the .txt file. Code is below:

import MySQLdb
text_file = open("configure.txt","r")
testCamp = (text_file.readline())
print testCamp
text_file.close()
db = MySQLdb.connect(host='localhost',user='username',passwd='password')
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
disp = cursor.fetchone()
print "Database Version: %s " %disp
db.close()
text_file = open(testCamp, "w")
text_file.write("Some Text\n")

input("\n\nPress Enter to Exit.")

As you can see from the code above I am in the process of setting up the framework so I'm not getting any data from the database yet. The new testCamp file is created ok (although it does not display the .txt extension instead it has some unfamiliar box icon after it). I have checked the file type and it does say it is a normal text file and I have also checked that the permissions allow writing and they do. Interestingly I also tried writing to a text file that was already in place:

text_file = open("Test.txt","w")
text_file.write("Some Text\n")

and it still did not work! Any suggestions would be appreciated

Upvotes: 0

Views: 150

Answers (2)

torek
torek

Reputation: 488083

The readline method on a stream (open file) retains the terminating newline "\n", so if the first line of configure.txt is test.txt, you will be writing to a file named "test.txt\n". Use:

testCamp = testCamp.rstrip()

(after reading the line) to remove all trailing white space (blanks, tabs, newlines), or equivalently:

testCamp = text_file.readline().rstrip()

The write method on a stream opened to a non-interactive file (such as "test.txt" or "test.txt\n") buffers (hangs on to) data. It gets pushed out—"flushed", as in exposing a bird when hunting (see meaning 3 of the verb form of flush)—by invoking the flush method or by closing the stream.

The with context manager will close the stream (as noted in comments and other answers). Use it for reading the configuration file, too (it works for read streams as well as write streams).

Upvotes: 1

jrd1
jrd1

Reputation: 10716

You forgot to close your file!

Use this:

text_file.close()

While this is perfectly fine, it is recommended that you use the following format when dealing with file I/O:

with open("filename", "mode(s)") as f:
    f.write("Data\n")

So, in your case, that would be:

with open(testCamp, "w") as text_file:
    text_file.write("Some Text\n")

And why use with? Read the following:

What is the python "with" statement designed for?

UPDATE 1:

As per @SukritKalra's suggestion, if you're using the context manager via with as shown, you don't need to close the file, that is taken care of for you.

UPDATE 2:

@Torek's answer is correct too, in that if you're using readline, you should strip the line before using it verbatim as the name of a file.

Upvotes: 0

Related Questions