How to Flush the Output of the Print Function

How can I flush the output of the print function?

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

print("Hello, World!", flush=True)

In Python 2, after calling print, do:

import sys
sys.stdout.flush()

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

What does print()'s `flush` do?

Normally output to a file or the console is buffered, with text output at least until you print a newline. The flush makes sure that any output that is buffered goes to the destination.

I do use it e.g. when I make a user prompt like Do you want to continue (Y/n):, before getting the input.

This can be simulated (on Ubuntu 12.4 using Python 2.7):

from __future__ import print_function

import sys
from time import sleep

fp = sys.stdout
print('Do you want to continue (Y/n): ', end='')
# fp.flush()
sleep(5)

If you run this, you will see that the prompt string does not show up until the sleep ends and the program exits. If you uncomment the line with flush, you will see the prompt and then have to wait 5 seconds for the program to finish

Set print flush=True to default?

You can use partial:

from functools import partial
print_flushed = partial(print, flush=True)
print_flushed("Hello world!")

From the documentation:

The partial() is used for partial function application which “freezes” some portion of a function’s arguments and/or keywords resulting in a new object with a simplified signature.

Why doesn't python3's print statement flush output when end keyword is specified?

In fact this is the default behavior of the underlying stdio functions.

When the output is to console, the stream will be automatically flushed when a newline is encountered, but not other characters.

If the output is not a console, then even newline won't trigger a flush.

If you want to make sure about flush, you can tell the print() explicitly:

print("Rewinding.......",end = '',flush=True)

In Python, how does flushing work and what is the reason?

When you print in Python and use as last character a comma (not as character but as separation sign), the output does not get flushed because it is waiting for another print statement. This is the function of the comma in the print command. If you do not add the comma to the print command it should flush and create a new line in the process, actually the other way around, it creates a newline and therefor flushes.

    print(task.get(), end=',') 

Will print without newline and waits for the next print that will create a new line, before flushing the buffer.

 print(task.get())  
print(task.get(), end='\n') # works well

Will print something including a new line and flushes the buffer.

This is very common practice in many console output applications, exactly the same behaviour you get with C and C++. The newline is used as a sign to the buffer to empty, if the buffer would empty after each character it would consume way too much cpu.

 print(task.get(), end=',', flush=True) 

The above works because we say explicitly to flush the buffer. There will be no newline but the print statement will flush the buffer and therefor print immediately.

Python: Is it possible to make 'print' flush by default?

For me this does looks like task for functools.partial, but I am not sure if it would work in this case, so can you please try following: add at begin of your file

import functools
print = functools.partial(print, flush=True)

and test if it does what you want?

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

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.

Python3: print(somestring,end='\r', flush=True) shows nothing

The problem is that the '\r' at the end clears the line that you just printed, what about?

import time
def show_Remaining_Time(time_delta):
print("\r", end='')
print('Time Remaining: %d' % time_delta, end='', flush=True)

if __name__ == '__main__':
count = 0
while True:
show_Remaining_Time(count)
count += 1
time.sleep(1)

In this way, you clear the line first, and then print the desired display, keeping it in screen for the duration of the sleep.

NOTE: The code above was modified to add the end='' as suggested in the comments for the code to work properly in some platforms. Thanks to other readers for helping to craft a more complete answer.



Related Topics



Leave a reply



Submit