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

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
Clarifying stdout flushing in Python when \\n appears inside a single print() call (C vs CPython behavior) - Documentation - Discussions on Python.org
In C, when stdout is line-buffered (for example, when connected to a terminal), encountering a ‘\n’ within a string causes the output buffer to flush immediately up to that point. As a result, developers often expect incremental flushing behavior when newline characters appear inside a ... More on discuss.python.org
🌐 discuss.python.org
0
January 17, 2026
shell - Write Python stdout to file immediately - Unix & Linux Stack Exchange
Alternatively explicit flushing of the output stream after each print should achieve the same. It looks like sys.stdout.flush() should achieve this in Python. More on unix.stackexchange.com
🌐 unix.stackexchange.com
February 2, 2015
Batched stdout handler doesn't get called when stdout is flushed
🐛 Bug I'm using a batched stdout handler, and I'm observing that my handler isn't getting called when sys.stdout.flush() is called in Python. The docs indicate that the handler should b... More on github.com
🌐 github.com
6
September 13, 2023
🌐
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?

🌐
Real Python
realpython.com › python-flush-print-output
How to Flush the Output of the Python Print Function – Real Python
January 25, 2025 - In this case, the data buffer flushes automatically when it encounters a newline character ("\n"): When interactive, the stdout stream is line-buffered. (Source) If you write your countdown script using print() with its default arguments, then the end of each call to print() writes a newline character implicitly: ... If you run this script from your terminal using the Python ...
🌐
Python.org
discuss.python.org › documentation
Clarifying stdout flushing in Python when \\n appears inside a single print() call (C vs CPython behavior) - Documentation - Discussions on Python.org
January 17, 2026 - Hi everyone, I would like to discuss a small but potentially confusing difference between C stdio and Python’s stdout behavior that may benefit from clearer documentation. In C, when stdout is line-buffered (for example, when connected to a terminal), encountering a ‘\n’ within a string causes the output buffer to flush immediately up to that point.
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.
🌐
GitHub
github.com › pyodide › pyodide › issues › 4139
Batched stdout handler doesn't get called when stdout is flushed · Issue #4139 · pyodide/pyodide
September 13, 2023 - The docs indicate that the handler should be called when stdout is flushed. Providing a batched stdout callback using setStdout:
Author   ejanzer
🌐
Python.org
discuss.python.org › python help
Why doesn't sys.stdout.flush() call os.fsync? - Python Help - Discussions on Python.org
September 15, 2023 - I generally would expect that sys.stdout.flush() calls os.fsync() but it does not. Pyodide implements a buffered output device that does not write out any data until fsync(1) is called or a newline is written. This output device is closely based on an output device in Emscripten.
🌐
Python
bugs.python.org › issue44415
Issue 44415: sys.stdout.flush and print() hanging - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/88581
🌐
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 - For instance, stdout is usually line-buffered when it is connected to a terminal and block-buffered when it is connected to a file. The flush() method is used to manually flush the internal buffer of a file or stream.
🌐
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!")
🌐
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.
🌐
Stack Abuse
stackabuse.com › bytes › flush-the-output-of-the-print-function-in-python
Flush the Output of the print() Function in Python
August 29, 2023 - That's where flushing comes in. When you flush the output buffer, you're telling Python to immediately write out any data that's currently stored in the buffer, even if the buffer isn't full.
🌐
Julia Programming Language
discourse.julialang.org › general usage
Set flushing mode for output stream - General Usage - Julia Programming Language
August 7, 2023 - Is there a way either 1) to specify the flushing mode of an existing output stream or 2) to create a new stream that has the desired flushing mode? [Edit: the output stream needs to be connected to the “screen” (console) and directable to a text file.] I’m still suffering from the fact that each time I want to print something immediately, I need to add flush(stdout) after the print statement: https://discourse.julialang.org/t/stderr-not-flushed-right-away I could use the @info facility in som...
🌐
Python
python-list.python.narkive.com › V954tRC4 › what-does-sys-stdout-flush-do
What does sys.stdout.flush() do?
Permalink Somewhere i read.. sys.stdout.flush(): Flush on a file object pushes out all the data that has been buffered to that point. Can someone post here a script example with sys.stdout.flush(), where in case i commented that i could understand what the difference really would be?
🌐
SysTutorials
systutorials.com › home › systutorials posts › how to flush stdout buffer in python?
How to flush STDOUT buffer in Python? - SysTutorials
November 21, 2019 - 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, stdout and stderr to be totally unbuffered...
🌐
GeeksforGeeks
geeksforgeeks.org › python › file-flush-method-in-python
File flush() method in Python - GeeksforGeeks
July 12, 2025 - The flush() method clears the internal buffer during write operations, ensuring data is immediately saved to a file. It doesn't apply to reading, as reading simply accesses data already stored in the file.
🌐
TestDriven.io
testdriven.io › tips › bbb44570-66d1-4ff5-af6a-5e3a2dc5e02a
Tips and Tricks - How to flush output of print in Python? | TestDriven.io
Python tip: You can set flush=True for the print() function to avoid buffering the output data and forcibly flush it: print("I'm awesome", flush=True) View All Tips · Feedback · × ·