Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to flush the output of the print function?

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

2 Answers

aashaykumar
In Python 3, print can take an optional flush argument:

print("Hello, World!", flush=True)
In Python 2 you'll have to do

import sys
sys.stdout.flush()

after calling print.
print prints to sys.stdout (see the documentation for more about file objects).
I hope this explains your question? Or do you need more explanation
Sonali7
In Python 3, call print(..., flush=True)
Or else Call file.flush() on the output file (we can wrap python 2's print function to do this)

Login / Signup to Answer the Question.