9

I have the following code to flushing out the output buffer.

print('return 1')
sys.stdout.flush()

Can I set up the print function so that it automatically flushes the buffer when it's called?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
prosseek
  • 182,215
  • 215
  • 566
  • 871
  • 1
    Hm, goofed up my link, should have been: [unbuffered stdout in python](http://stackoverflow.com/questions/881696/unbuffered-stdout-in-python-as-in-python-u-from-within-the-program) – eldarerathis Oct 09 '10 at 03:05

1 Answers1

15

You can start python in unbuffered mode using the -u flag, e.g.

python -u script.py

or

#!/usr/bin/env python -u

as "shebang" header for your script.

Imran
  • 87,203
  • 23
  • 98
  • 131
Ivo van der Wijk
  • 16,341
  • 4
  • 43
  • 57