Problems with file.write()

Jeremy Yallop jeremy at
Sat Aug 31 09:11:26 EDT 2002

* Marc
| I changed the buffering mode when I opened the file, and it definitely
| helped. Now I get almost the whole file, everything except for the
| last three or four lines. So I guess I do have some type of buffering
| problem. I don't understand why this happens though. I've never had
| this problem before, and I've worked quite a bit with files.
| So now how do I get those last few lines? And if I should place a
| strategic .flush() somewhere, where should it go?

Call flush() when you want the data to be written to disk.  From your
description this may be as frequently as after every call to write().
However, if you're going to do that then you might as well open the
file in unbuffered mode, since you've lost the performance advantages
of buffering anyway:

  fd = open(filename, 'w', 0)

Note that all buffered data is flushed automatically when you close()
an output file (you *are* closing the file, aren't you?).


More information about the Python-list mailing list