Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How often does python flush to a file?

Tags:

python

file

flush

  1. How often does Python flush to a file?
  2. How often does Python flush to stdout?

I'm unsure about (1).

As for (2), I believe Python flushes to stdout after every new line. But, if you overload stdout to be to a file, does it flush as often?

like image 471
Tim McJilton Avatar asked Jul 02 '10 16:07

Tim McJilton


2 Answers

For file operations, Python uses the operating system's default buffering unless you configure it do otherwise. You can specify a buffer size, unbuffered, or line buffered.

For example, the open function takes a buffer size argument.

http://docs.python.org/library/functions.html#open

"The optional buffering argument specifies the file’s desired buffer size:"

  • 0 means unbuffered,
  • 1 means line buffered,
  • any other positive value means use a buffer of (approximately) that size.
  • A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files.
  • If omitted, the system default is used.

code:

bufsize = 0 f = open('file.txt', 'w', buffering=bufsize) 
like image 191
Corey Goldberg Avatar answered Oct 08 '22 10:10

Corey Goldberg


You can also force flush the buffer to a file programmatically with the flush() method.

with open('out.log', 'w+') as f:     f.write('output is ')     # some work     s = 'OK.'     f.write(s)     f.write('\n')     f.flush()     # some other work     f.write('done\n')     f.flush() 

I have found this useful when tailing an output file with tail -f.

like image 43
kortina Avatar answered Oct 08 '22 10:10

kortina