Clearing file object buffers

D

disgracelands

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?

thanks,
dis
 
P

Peter Hansen

disgracelands said:
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?

fileobject.flush() will do the trick.

-Peter
 
J

John J. Lee

disgracelands said:
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?

python's -u command-line switch during debugging can be useful.


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top