fo.flush() & fo.write() on windows

Tim Peters tim_one at email.msn.com
Mon Aug 30 02:25:30 EDT 1999


[Tim]
>> file.write() always returns None, as documented.  Don't know what
>> you expected it to return, but read the docs.

[Roberts, Robert J]
> I must have been reading the wrong place in the documentation all
> this time. I was reading doc/lib/node105.html ...

Python offers a rich variety of I/O primitives (how's that for positive
phrasing <wink>?).  You're using the .write() method of file objects, but
reading the docs for the low-level os.write function, which is a different
beast entirely.

Alas, this stuff is spread out.  The docs for the builtin "open" function (and
its optional 3rd buffering argument) are in section 2.3 (Built-in Functions).
The docs for file object methods are in section 2.1.7.9 (File Objects).

>> The only way to get [writes committed to disk] under Windows is to
>> pass the non-standard "c" (for Commit) mode flag to "open":
>>
>>     fo = open(foName, 'ac')
>>
>> [and then they still may not show up on disk until you do fo.flush()]

> I'll look into this too, but I don't want to slow the code down
> anymore if I can help it. I'll compare it with closing and reopening
> the file (which DOES dump the buffer immediately) just to see what
> happens.

Probably depends on whether you need stuff reflected on disk after every single
write, or can afford to bunch up a few writes before flushing.  close+open is
certainly more portable, but more likely slower more often.  OTOH, I don't see
anything in MS's tech docs that *promises* closing a file will immediately
write it to disk; they do promise flush will write to disk immediately if you
use "c" mode.

windows-it's-not-just-an-os-it's-an-adventure-ly y'rs  - tim






More information about the Python-list mailing list