File flush() method in Python
The flush() method in Python is used to quickly send data from the computer's temporary storage, known as a buffer, to a file or the screen. Normally, when you write something in Python, it doesn't get saved or shown right away. It stays in a buffer for a short time to make things faster. But sometimes, you want to make sure that the data is written or shown immediately. For Example:
import time
with open("gfg.txt", "w") as file:
file.write("First line\n")
file.flush() # immediately write to disk
print("First line written and flushed.")
time.sleep(2)
file.write("Second line\n")
Output
First line written and flushed.When the program finishes, gfg.txt will contains

Explanation: This code opens gfg.txt, creating or overwriting it. It writes the first line to the buffer and immediately flushes it to disk. After a 2-second pause, the second line is written to the buffer, but it is only saved to disk when the file is automatically closed at the end of the with block.
Syntax of file flush() method
file_object.flush()
Or when used with print():
print(object, ..., flush=True)
Parameters:
- flush() method does not take any parameters, it simply forces the buffered data to be written to the file immediately.
- For print(object, ..., flush=True): flush (Optional, default: False) when True, it forces the output buffer to flush immediately, displaying the printed output instantly.
Returns:
- file.flush(): This method does not return any value.
- print(object, ..., flush=True): This also does not return any value, even with the flush=True argument.
Examples of file flush() method
Example 1: This example simulates task progress using a loop. flush() ensures each step is instantly written to the file and console without delay.
import time
with open("progress.txt", "w") as f:
for i in range(1, 4):
f.write(f"Step {i} completed\n")
f.flush() # Write to file immediately
print(f"Step {i} completed", flush=True) # Console output
time.sleep(1) # Simulate delay
Output


Explanation: This code writes progress updates for three steps. Each step is immediately saved to a file and printed to the console without delay, using flush(). A short pause simulates processing time between steps.
Example 2: This example simulates item processing and uses flush=True with print() to instantly display each update in the console without buffering delays.
import time
for i in range(3):
print(f"Processing item {i+1}", flush=True) # Print immediately
time.sleep(1) # Simulate processing delay
Output

Explanation: This code simulates processing three items. Each item's status is printed to the console instantly using flush=True, and a 1-second pause mimics processing time between updates.
Why we can't use flush() for reading
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. To control where to read from, use the seek() method to move the file cursor to a specific position, allowing you to read from any part of the file. Example:
with open('example1.txt', 'r') as file:
file.seek(10) # Move cursor to 10th byte
c = file.read()
print(c)
Output
Hello, this is an example file.
It has multiple lines of text.

Explanation: This code opens example1.txt, uses seek(10) to move the cursor to the 10th byte and then reads and prints the content starting from that position, skipping the first 10 bytes.
Learn More, File Handling in Python