Hi, I am writing a script that aims to help a person keep a list of characters while reading a book. In doing this, I am writing to a file and checking it constantly, except I ran into a problem. Say I executed a code block such as the following:

infoToWrite = "Harry Potter: Teenage boy who has an owl"
myFile = open("characterlist.txt", "r+")
myFile.write(infoToWrite)

Now after this code is executed, I notice that my "characterlist.txt" file does not contain the data I just wrote. However, if I were to close the Python shell, the file contains the data.

So, what is causing this? And is there anything I would be able to do to make this file writing instantaneous?

Thanks in advance.

Hi, I am writing a script that aims to help a person keep a list of characters while reading a book. In doing this, I am writing to a file and checking it constantly, except I ran into a problem. Say I executed a code block such as the following:

infoToWrite = "Harry Potter: Teenage boy who has an owl"
myFile = open("characterlist.txt", "r+")
myFile.write(infoToWrite)

Now after this code is executed, I notice that my "characterlist.txt" file does not contain the data I just wrote. However, if I were to close the Python shell, the file contains the data.

So, what is causing this? And is there anything I would be able to do to make this file writing instantaneous?

Thanks in advance.

Try:

infoToWrite = "Harry Potter: Teenage boy who has an owl"
myFile = open("characterlist.txt", "r")
myFile.write(infoToWrite)

It should work, I only removed the 'plus'(+) after the r in 'myFile'

Try:

infoToWrite = "Harry Potter: Teenage boy who has an owl"
myFile = open("characterlist.txt", "r")
myFile.write(infoToWrite)

It should work, I only removed the 'plus'(+) after the r in 'myFile'

Thanks for an idea, except how would that work? Isn't "r" for just reading? If I do what you suggested, I am greeted with the following error:

IOError: [Errno 9] Bad file descriptor

try this:

infoToWrite = "Harry Potter: Teenage boy who has an owl"
myFile = open("characterlist.txt",'r+')
myFile.write(infoToWrite)
myFile.flush()

you can put a raw_input() line beneath flush() and check if it's writing to file

try this:

infoToWrite = "Harry Potter: Teenage boy who has an owl"
myFile = open("characterlist.txt",'r+')
myFile.write(infoToWrite)
myFile.flush()

you can put a raw_input() line beneath flush() and check if it's writing to file

Thank you so much! It would have been a pain to close and re-open the file every time I wrote to the file. Solved my problem. Thanks again.

What would opening it as unbuffered do? And would it have a large effect on performance?

Opening it unbuffered means that when you call the file's write function the data will be written to disk immediately. This means that you could potentially be accessing the hard drive more frequently. If you are calling flush after each write already though then there won't be any difference there.

You can also open a file in line buffer mode by changing the '0' to a '1'. As the name implies this will buffer writing until you've written an entire line. Depending on what you're doing that might be a good middle ground.

Comments
Continued, quality support.
This question has already been answered. Start a new discussion instead.