[Pythonmac-SIG] Frustration building, please help...

Russell E Owen owen@astro.washington.edu
Fri, 2 Nov 2001 09:34:08 -0800


Others have already answered the overall question, but there is one more point I'd like to note: if you are going to use 'r+' mode, you must be sure to flush all output to the file before attempting to read the file.

I don't believe that f.write() and f.writelines() are guaranteed to flush their output, so I would recommend you do it explicitly with f.flush() before attempting any read (and then seek to the beginning of the file). In other words:
f.write...
f.flush()
f.seek(0)
f.read...

If you don't mind writing more, I'd be curious what application you have, for which you are using a file that is simultaneously open for read and write. (Both out of curiosity -- it's something I've thought about using over the years but never actually ended up doing -- and because we might be able to suggest a simpler design).

-- Russell