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

Roberts, Robert J robert_j_roberts at rl.gov
Sun Aug 29 21:27:46 EDT 1999


Tim Peters wrote:

> [Roberts, Robert J]
> > Am I asking too much?
>
> On Windows, yes.  C's stdio isn't its native file-handling system, and its
> implementation of stdio is, umm, "surprising".
>
> > pseudo code:
>
> >     fo = __builtin__.open(foName, 'a')
>
> Why not plain "open"?

Because __builtin__.open() works? I started doing file opens by copying some of
Guido's code. It worked. I never tried open() alone.

>
>
> >     foSize = os.stat(foName)[6]     # if foSize = 100...
>
> os.path.getsize(foName) is easier on the brain (new in 1.5.2 though).

Thanks. Didn't know it was an option.

>
>
> >     fo.write('Some string.')             # fo.write() returns None
>
> file.write() always returns None, as documented.  Don't know what you expected
> it to return, but read the docs.

I must have been reading the wrong place in the documentation all this time. I was
reading doc/lib/node105.html which says:

"write(fd,str)  Write the string str to file descriptor fd. Return the number of
bytes actually written."

>
>
> >     foSize = os.stat(foName)[6]    # ...foSize STILL = 100.
>
> Unsurprising so far.
>
> >     fo.flush()
> >     foSize = os.stat(foName)[6]    # ...foSize STILL = 100.
>
> Ah -- *that's* Windows <wink>!
>
> >     fo.close()                     # 'Some string' appears in fo.
> >     foSize = os.stat(foName)[6]    # [now the bytes appear]
> >
> > I am running Python 1.5.2 on Windows 95. fo.write() appears to be
> > buffering the write and returning None for bytes written on
> > fo.write().
>
> "None" is expected (see above).  Buffering should be expected too since you
> opened the file with buffering.  If you don't want buffering, pass 0 as the
> third arg:
>
>      fo = open(foName, 'a', 0)

Didn't know this was an option. I'll give it a try and see what it does. But (as
you say), it probably won't do much if Windows is still buffering.

>
>
> Alas, on Windows that won't make much of a difference in what you see.
>
> > fo.flush() appears to do nothing.
>
> MS's implementation of stdio's fflush (which Python's file.flush() calls
> directly) does clear the stdio buffers, but merely flushes them to Windows'
> internal buffers.  It does not imply commitment to disk.  The only way to get
> that under Windows is to pass the non-standard "c" (for Commit) mode flag to
> "open":
>
>     fo = open(foName, 'ac')

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.

>
>
> Then fo.write(whatever) *still* won't (necessarily) show up on disk right away,
> but fo.flush() will work the way you expect.  "c" is unportable, and much as
> you may think you want it now <wink>, slows down Windows output enormously.
>
> windows-was-designed-under-the-assumption-that-it-would-
>     never-crash<snort>-ly y'rs  - tim

Thanks, millions!





More information about the Python-list mailing list