Python – How to flush the output of the print function


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

Best Solution

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

after calling print. By default, print prints to sys.stdout (see the documentation for more about file objects).