How can I flush the output of the print function?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python's print() function buffers output by default, meaning text may not appear immediately on the screen or in log files. This becomes a problem when monitoring long-running scripts, debugging, or piping output to other processes. To force immediate output, use the flush=True parameter, run Python with -u flag, or set the PYTHONUNBUFFERED environment variable.
Using flush=True (Per Call)
The flush=True parameter forces Python to write the buffer to the output stream immediately after each print() call.
Using sys.stdout.flush()
Call flush() on the stream directly:
This is equivalent to flush=True but useful when you cannot modify the print() call itself.
Unbuffered Mode (Global)
Disable buffering for the entire Python process:
With -u or PYTHONUNBUFFERED=1, all stdout and stderr output is unbuffered. This is the standard approach for Docker containers and CI pipelines where you want logs to appear in real time.
Overriding print Globally
Replace the built-in print with a flushing version:
Or redirect stdout through an unbuffered wrapper:
Writing to Files
File output is also buffered. Flush when writing logs:
Progress Indicators
Flushing is essential for progress bars and status updates:
Without flush=True, the \r carriage return trick does not work — the output stays in the buffer until a newline is printed.
When Buffering Matters
Common Pitfalls
- Forgetting to flush when using
end="": When you suppress the newline withend="", Python does not flush automatically (line buffering only flushes on newlines). Always addflush=Truewhen usingend=""orend=" ". - Buffered output in Docker containers: Docker captures stdout in blocks. Without
PYTHONUNBUFFERED=1in your Dockerfile orpython -u, logs appear delayed or not at all indocker logs. Always set this for containerized Python apps. - Assuming stderr is buffered:
sys.stderris unbuffered by default in Python. Printing to stderr (print(..., file=sys.stderr)) always appears immediately without needingflush=True. - Performance impact of always flushing: Flushing after every print call adds I/O overhead. For performance-critical code that writes thousands of lines per second, flush periodically (every N lines) rather than on every call.
- Confusing
printbuffering with file buffering:print()writes tosys.stdout, which has its own buffer. Writing to a file withopen()has a separate buffer. Flushingsys.stdoutdoes not flush file buffers and vice versa.
Summary
- Use
print("msg", flush=True)to flush output immediately on a per-call basis - Use
python -uorPYTHONUNBUFFERED=1for globally unbuffered output - Flushing is essential for progress bars,
\rline overwrites, and real-time log monitoring - Always set
PYTHONUNBUFFERED=1in Docker containers for visible logs sys.stderris unbuffered by default — onlysys.stdoutneeds explicit flushing- For files, use
f.flush()orbuffering=1(line buffering) for real-time writes
Related reading
- How can I flush the output of the print function?
- How can I force socket opened with python to close?
- How can I generate a unique ID in Python?
- How can I get a list of all classes within current module in Python?
- How can I get a random key-value pair from a dictionary?
- How can I get a value from a cell of a dataframe?
- How can I get current CPU and RAM usage in Python?
- How can I get dictionary key as variable directly in Python not by searching from value?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.