Usage of sys.stdout.flush() method

I159 picture I159 · Apr 4, 2012 · Viewed 193.3k times · Source

What does sys.stdout.flush() do?

Answer

Haldean Brown picture Haldean Brown · Apr 4, 2012

Python's standard out is buffered (meaning that it collects some of the data "written" to standard out before it writes it to the terminal). Calling sys.stdout.flush() forces it to "flush" the buffer, meaning that it will write everything in the buffer to the terminal, even if normally it would wait before doing so.

Here's some good information about (un)buffered I/O and why it's useful:
http://en.wikipedia.org/wiki/Data_buffer
Buffered vs unbuffered IO