Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's print function that flushes the buffer when it's called? [duplicate]

Tags:

python

flush

Possible Duplicates:
How to flush output of Python print?
unbuffered stdout in python (as in python -u) from within the program

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

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

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

like image 966
prosseek Avatar asked Oct 09 '10 02:10

prosseek


1 Answers

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.

like image 181
Ivo van der Wijk Avatar answered Sep 22 '22 04:09

Ivo van der Wijk