Clearing file object buffers

Pettersen, Bjorn S BjornPettersen at fairisaac.com
Wed Sep 17 23:54:23 CEST 2003


> From: disgracelands [mailto:disgracelands at quietblue.co.uk] 
> 
> In my code i have setup sys.stdout and sys.stderr as file 
> objects so that i
> can easily track whats going on during the course of my 
> program.  So far its
> proving useful but the objects seem to be buffering any text 
> supplied to
> their write() functions and when i hit an error condition and 
> have to exit
> early they fail to write and i'm left with empty files.
> 
> Is there any way to force the file objects to flush the 
> buffer to file on
> disc?

>>> import sys
>>> dir(sys.stdout)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
'__init__', '__ite
r__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__', 'clo
se', 'closed', 'encoding', 'fileno', 'flush', 'isatty', 'mode', 'name',
'newlines', 'next'
, 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace',
'tell', 'truncate', 'w
rite', 'writelines', 'xreadlines']
>>> print sys.stdout.flush.__doc__
flush() -> None.  Flush the internal I/O buffer.
>>>

hth,
-- bjorn





More information about the Python-list mailing list