October 14, 2009

Python tips and tricks: Flushing stdout

Many a times I need to redirect the output of a program to a file. For example

#> python test.py > outfile

However, the problem with this is that stdout is not flushed on each print command and some of the statements can still be in the buffer which can be painful at times based on the level of anxiety :).

We shall now see one of the quickest way to ensure that stdout is flushed. I have not seen a case where it didn't work (at least in Linux) and do not know how one can verify if this works at all times.

For example if your file is as follows:
  • def test():
    • j = True
    • while j:
      • time.sleep(1)
      • print("Hello:"+str(j))
    • print "Bye"
  • if __name__ == "__main__":
    • func()

Just add the following line to your code
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

This can be done as follows:
  • if __name__ == "__main__":
    • import os, sys
    • sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
    • func()


shmeedogg said...

Why not just sys.stdout.flush()?

Ashwin Rao said...

You would have to do it with each print statement, or at specific points where a flush was necessary, else it would be in the buffers.

Anonymous said...

python -u ...

excerpt from 'python -h'

-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x
see man page for details on internal buffering relating to '-u'


Anonymous said...

Just pass additional param to print i.e print('Message', flush=True) # for python 3