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).

Answer from CesarB on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sys-stdout-flush
Python - sys.stdout.flush() - GeeksforGeeks
July 12, 2025 - Another way of achieving the same functionality as above is setting the flush parameter of the print statement to true. ... # Python3 program demonstrating working # of flush during output and usage of # flush parameter of print statement import sys import time for i in range(10): print(i, end =' ', flush = True) time.sleep(1)
🌐
Real Python
realpython.com › python-flush-print-output
How to Flush the Output of the Python Print Function – Real Python
January 25, 2025 - By the end of this tutorial, you’ll understand that: Flush in coding refers to emptying the data buffer to ensure immediate output. flush=True in print() forces the buffer to clear immediately.
Discussions

python - How can I flush the output of the print function? - Stack Overflow
I have many prints in my file and don't want to change them + I want my files to always flush and I don't want to write it ever. Just always flush is what I want. Will putting sys.stdout.flush() that at the top be enough? (I am using python 3 and above) 2021-04-02T20:01:02.22Z+00:00 More on stackoverflow.com
🌐 stackoverflow.com
python - Usage of sys.stdout.flush() method - Stack Overflow
Python's standard out is buffered (meaning that it collects some of the data "written" to standard out before it writes it to the terminal). Calling sys.stdout.flush() forces it to "flush" the buffer, meaning that it will write everything in the buffer to the terminal, even if normally it would ... More on stackoverflow.com
🌐 stackoverflow.com
What does sys.stdout.flush() do? ELI5
I think this explanation is close to ELI5 https://www.geeksforgeeks.org/python-sys-stdout-flush/ More on reddit.com
🌐 r/learnpython
16
1
July 21, 2023
How to control stdout buffering
Not sure, but if all else fails why not "print" to a string and then "flush" it with an actual print to the console? More on reddit.com
🌐 r/learnpython
9
4
February 5, 2020
🌐
SysTutorials
systutorials.com › home › systutorials posts › how to flush stdout buffer in python?
How to flush STDOUT buffer in Python? - SysTutorials
March 24, 2018 - flush() Flush the write buffers of the stream if applicable. This does nothing for read-only and non-blocking streams. If you can’t change the code while you can change the python interpreter options used, you can give it -u: -u Force stdin, ...
🌐
Reddit
reddit.com › r/learnpython › what does sys.stdout.flush() do? eli5
r/learnpython on Reddit: What does sys.stdout.flush() do? ELI5
July 21, 2023 -

I'm writing a library to do some stuff with an API. I've put in some error handling to avoid the thing blowing up on the rare occasion when the API doesn't return properly using some code I found on stack exchange.

    respi=requests.get(f"{burl}/{searchtype}/{iid}")
    notdone=True
    retries=0
    while notdone:
        try:    
            iinfo=json.loads(respi.text)
            latlon=(iinfo['geo']['latitude'],iinfo['geo']['longitude'])
            notdone=False
        except Exception as e:
            if retries==5:
                print("Too many retries")
                print("Exiting....")
                sys.exit()
            wait=(retries+1)**2
            print(f'Something went wrong.... retrying in {wait} seconds')
            sys.stdout.flush()
            time.sleep(wait)
            retries+=1       
    time.sleep(0.1)

The question I have is, what does sys.stdout.flush() actually do here?

🌐
Delft Stack
delftstack.com › home › howto › python › python print flush
How to Flush Print Output in Python | Delft Stack
March 11, 2025 - This tutorial explains how to flush print output in Python, ensuring immediate visibility of your print statements. Learn about the flush parameter, sys.stdout.flush(), and context managers to control output effectively. Enhance your Python programming skills with these essential techniques for real-time feedback in console applications.
Find elsewhere
🌐
DEV Community
dev.to › marvintensuan › pythons-print-and-the-flush-parameter-3d7k
Python's print and the flush parameter. - DEV Community
January 15, 2022 - Personally, I was aware of sys.stdout.write existing. But for now, we can be comfortable knowing that terminal output is pretty much like a regular text file being read from somewhere. Hence, it makes sense that print, by default, would prefer to complete a line first before giving an output. ... Setting PYTHONUNBUFFERED via command line. I use Windows so in PowerShell, it's something like $env:PYTHONUNBUFFERED = "TRUE". This overrides flush=False.
🌐
Medium
medium.com › @hhtg250 › stdin-stdout-flush-and-buffering-in-python-e747b85cb6ae
Understanding stdin, stdout, flush(), and Buffering in Python | by Ahmed Nabil - أحمد نبيل | Medium
July 25, 2024 - In Python, handling input and output efficiently is crucial for various applications, from simple scripts to complex data processing. This article delves into the concepts of stdin, stdout, flush(), and buffering, explaining how they work and when to use them.
🌐
CodeRivers
coderivers.org › blog › python-flush-stdout
Python Flush STDOUT: A Comprehensive Guide - CodeRivers
March 25, 2025 - This blog post will explore the fundamental concepts, usage methods, common practices, and best practices related to flushing STDOUT in Python.
🌐
Medium
medium.com › @ryan_forrester_ › python-print-flush-complete-guide-b10ab1512390
Python Print Flush: Complete Guide | by ryan | Medium
October 30, 2024 - This script shows: 1. Current progress updates in real-time 2. Uses `\r` to update the same line 3. Left-aligns filenames with `:❤0` for clean formatting 4. Flushes output for immediate updates ... import time import sys def spinning_cursor(): """Create a spinning cursor animation""" cursors = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'] for _ in range(50): # Spin 50 times for cursor in cursors: # Print cursor and go back one character sys.stdout.write(cursor) sys.stdout.flush() time.sleep(0.1) sys.stdout.write('\b') # Run the animation print("Loading ", end='') spinning_cursor() print(" Done!")
🌐
Real Python
realpython.com › videos › sep-end-and-flush
sep, end, and flush (Video) – Real Python
The first and second print() functions here will put things into the buffer with commas between the words and commas at the end, and the third print() puts commas between the words and flushes the buffer with a '\n'. 06:59 This last example shows you how you can combine '\n' with other characters to make things look like a bullet list. 07:11 Up until now, I’ve only been printing to the screen. This is called the stdout (standard out) stream.
Published   September 5, 2020
🌐
Better Stack
betterstack.com › community › questions › how-to-flush-print-in-python
How can I flush the output of the print function in Python? | Better Stack Community
February 3, 2023 - In Python, you can use the flush method of the sys.stdout object to flush the output of the print function.
🌐
GitHub
gist.github.com › fsword › 36a9395d8c11a4a72361
python stdout auto flush · GitHub
python stdout auto flush · Raw · output.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
Python
python-list.python.narkive.com › V954tRC4 › what-does-sys-stdout-flush-do
What does sys.stdout.flush() do?
Use time.sleep() to see the effects of (line) buffering. Type the following in your interactive interpreter and be enlightened ;) · Post by D. Xenakis Can someone post here a script example with sys.stdout.flush(), where in case i commented that i could understand what the difference really ...
🌐
W3docs
w3docs.com › python
How can I flush the output of the print function?
Watch a video course Python - The Practical Guide · You can also use the flush method of the sys.stdout object to flush the output buffer: import sys print("This output will be buffered") sys.stdout.flush() Try it Yourself » · Copy · Keep ...
🌐
ZetCode
zetcode.com › python › flush
Python flush Function - Complete Guide
March 26, 2025 - Complete guide to Python's flush function covering file operations, buffering, and practical usage examples.
🌐
GeeksforGeeks
origin.geeksforgeeks.org › python-sys-stdout-flush
Python - sys.stdout.flush() - GeeksforGeeks
April 21, 2020 - This is because calling sys.stdout.flush() forces it to “flush” the buffer, meaning that it will write everything in the buffer to the terminal, even if normally it would wait before doing so.
🌐
GeeksforGeeks
geeksforgeeks.org › python › sys-stdout-write-in-python
sys.stdout.write in Python - GeeksforGeeks
July 15, 2025 - Python · import sys import time for i in range(5, 0, -1): sys.stdout.write(f'\rCountdown: {i} ') sys.stdout.flush() time.sleep(1) sys.stdout.write("\nTime's up!\n") # Use double quotes to avoid conflict with the apostrophe · Output · dynamic countdown · Explanation: \r (carriage return) moves the cursor to the start of the line.
🌐
Google Groups
groups.google.com › g › sage-devel › c › aGX7GlvkAWg › m › dMGG4T_GDwAJ
Python 3 and flushing output from external libraries
So maybe it should be on stderr, but it's not. Regarding sys.stdout.flush(), my understanding, as confirmed by my experience with this particular problem, is that this only flushes output coming from Python, not from external library calls.