Is output buffering enabled by default in Python’s interpreter for sys.stdout
?
If the answer is positive, what are all the ways to disable it?
Suggestions so far:
- Use the
-u
command line switch - Wrap
sys.stdout
in an object that flushes after every write - Set
PYTHONUNBUFFERED
env var sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
Is there any other way to set some global flag in sys
/sys.stdout
programmatically during execution?
16 s
I would rather put my answer in How to flush output of print function? or in Python’s print function that flushes the buffer when it’s called?, but since they were marked as duplicates of this one (what I do not agree), I’ll answer it here.
Since Python 3.3, print() supports the keyword argument “flush” (see documentation):
print('Hello World!', flush=True)