Programming
Python
Print Function
Output Flush
Coding Tips

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.

Browse interview questions

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)

python
1import time
2
3# Without flush — output may appear all at once at the end
4for i in range(5):
5    print(f"Processing {i}...", end=" ")
6    time.sleep(1)
7# May show: "Processing 0... Processing 1... Processing 2... " all at once
8
9# With flush — each print appears immediately
10for i in range(5):
11    print(f"Processing {i}...", end=" ", flush=True)
12    time.sleep(1)
13# Shows each "Processing N..." as it happens

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:

python
1import sys
2import time
3
4for i in range(5):
5    print(f"Step {i}")
6    sys.stdout.flush()  # Force output after each print
7    time.sleep(1)
8
9# Or write directly to stdout
10sys.stdout.write(f"Progress: {i}\n")
11sys.stdout.flush()

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:

bash
1# Method 1: -u flag
2python -u script.py
3
4# Method 2: Environment variable
5PYTHONUNBUFFERED=1 python script.py
6
7# Method 3: In Docker
8ENV PYTHONUNBUFFERED=1

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:

python
1import functools
2
3# Override print to always flush
4print = functools.partial(print, flush=True)
5
6# Now all print calls flush automatically
7print("This flushes immediately")
8print("So does this")

Or redirect stdout through an unbuffered wrapper:

python
1import sys
2import io
3
4# Replace stdout with an unbuffered version
5sys.stdout = io.TextIOWrapper(
6    open(sys.stdout.fileno(), 'wb', 0),
7    write_through=True
8)
9
10print("Unbuffered output")

Writing to Files

File output is also buffered. Flush when writing logs:

python
1with open("output.log", "w") as f:
2    for i in range(10):
3        f.write(f"Line {i}\n")
4        f.flush()  # Write to disk immediately
5
6# Or open with no buffering (binary mode only)
7with open("output.log", "wb", buffering=0) as f:
8    f.write(b"Unbuffered binary write\n")
9
10# Text mode with line buffering
11with open("output.log", "w", buffering=1) as f:
12    f.write("Flushes after each newline\n")  # Flushed immediately

Progress Indicators

Flushing is essential for progress bars and status updates:

python
1import time
2
3# Overwriting the same line
4for i in range(101):
5    print(f"\rProgress: {i}%", end="", flush=True)
6    time.sleep(0.05)
7print()  # Newline after completion
8
9# Spinner
10import itertools
11spinner = itertools.cycle(['|', '/', '-', '\\'])
12for _ in range(50):
13    print(f"\rWorking {next(spinner)}", end="", flush=True)
14    time.sleep(0.1)

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

python
1# Piping output to another process
2# python script.py | tee output.log
3# Without flushing, output appears in chunks, not line-by-line
4
5# Subprocess output capture
6import subprocess
7proc = subprocess.Popen(
8    ["python", "-u", "worker.py"],  # -u for unbuffered child
9    stdout=subprocess.PIPE,
10    text=True
11)
12for line in proc.stdout:
13    print(f"Worker: {line.strip()}")
14
15# Logging to a file that's tailed
16# tail -f output.log
17# Without flushing, tail shows nothing until the buffer fills

Common Pitfalls

  • Forgetting to flush when using end="": When you suppress the newline with end="", Python does not flush automatically (line buffering only flushes on newlines). Always add flush=True when using end="" or end=" ".
  • Buffered output in Docker containers: Docker captures stdout in blocks. Without PYTHONUNBUFFERED=1 in your Dockerfile or python -u, logs appear delayed or not at all in docker logs. Always set this for containerized Python apps.
  • Assuming stderr is buffered: sys.stderr is unbuffered by default in Python. Printing to stderr (print(..., file=sys.stderr)) always appears immediately without needing flush=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 print buffering with file buffering: print() writes to sys.stdout, which has its own buffer. Writing to a file with open() has a separate buffer. Flushing sys.stdout does not flush file buffers and vice versa.

Summary

  • Use print("msg", flush=True) to flush output immediately on a per-call basis
  • Use python -u or PYTHONUNBUFFERED=1 for globally unbuffered output
  • Flushing is essential for progress bars, \r line overwrites, and real-time log monitoring
  • Always set PYTHONUNBUFFERED=1 in Docker containers for visible logs
  • sys.stderr is unbuffered by default — only sys.stdout needs explicit flushing
  • For files, use f.flush() or buffering=1 (line buffering) for real-time writes

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.