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.

strange because it works in my compiler i used komodo 5.0 with python 2.6
have a look at this you might find something
http://bugs.python.org/issue3207#msg68773
maybe you should close the the file after you write in the file.

Ok thanks for the idea.

regarding that link i posted on the last poster suggests flushing the stream.

Member Avatar for sravan953

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.

You can also solve this problem by opening the file as unbuffered: "open('filename', 'w', 0)"

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.

commented: Continued, quality support. +1

I will have to try that. Thanks for the idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.