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

Gordon McMillan gmcm at hypernet.com
Mon Aug 30 09:37:47 EDT 1999


[The Timbot]
> [Roberts, Robert J]

> >     foSize = os.stat(foName)[6]     # if foSize = 100...
> >     fo.write('Some string.')             # fo.write() returns None
> >     fo.flush()
> >     foSize = os.stat(foName)[6]    # ...foSize STILL = 100.
> 
> Ah -- *that's* Windows <wink>!

Curiously enough, NT4 (using NTFS) behaves much more sensibly:

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam 
>>> f = open('temp.txt','a') 
>>> f.write('some string') 
>>> import os 
>>> os.path.getsize('temp.txt') 
0 
>>> f.flush() 
>>> os.path.getsize('temp.txt') 
11 
>>>

To the original poster: using f.tell() (after f.seek, if necessary) 
is a much more sensible (but not gotcha-free*) way of dealing with 
the size of an open file(**).

(*) It's not gotcha-free in text mode because it counts line endings 
as one (logical) char, while in fact you get 2 physical chars. 

(**)On really old Windows file systems, text files grow (from the
os.path perspective) in chunks. 

- Gordon




More information about the Python-list mailing list