Force flushing buffers
Scott David Daniels
scott.daniels at acm.org
Thu Oct 6 20:01:44 EDT 2005
Madhusudan Singh wrote:
> I have a python application that writes a lot of data to a bunch of files
> from inside a loop. Sometimes, the application has to be interrupted and I
> find that a lot of data has not yet been writen (and hence is lost). How do
> I flush the buffer and force python to write the buffers to the files ? I
> intend to put this inside the loop.
>
> Thanks.
...
files_to_close = []
try:
somefile = open(somename, 'w')
files_to_close.append(somefile)
... calculations, probably in a loop ...
finally:
for afile in files_to_close:
afile.close()
...
--Scott David Daniels
scott.daniels at acm.org
More information about the Python-list
mailing list