How to flush output of print function?


Question

How do I force Python's print function to output to the screen?

This is not a duplicate of Disable output buffering - the linked question is attempting unbuffered output, while this is more general. The top answers in that question are too powerful or involved for this one (they're not good answers for this), and this question can be found on Google by a relative newbie.

1
1069
6/7/2018 1:50:44 AM

Accepted Answer

import sys
sys.stdout.flush()

print by default prints to sys.stdout.

References

Python 2

Python 3

1249
4/30/2019 9:43:41 PM

Running python -h, I see a command line option:

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

Here is the relevant doc.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon