File flush() method in Python
Last Updated :
15 Apr, 2025
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:
Python
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

gfg.txt file
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.
Python
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

Terminal Output

progress.txt file
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.
Python
import time
for i in range(3):
print(f"Processing item {i+1}", flush=True) # Print immediately
time.sleep(1) # Simulate processing delay
Output

Terminal 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:
Python
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.

example1.txt file
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
Similar Reads
File Mode in Python
In Python, the file mode specifies the purpose and the operations that can be performed on a file when it is opened. When you open a file using the open() function, you can specify the file mode as the second argument. Different File Mode in PythonBelow are the different types of file modes in Pytho
5 min read
Python List methods
Python list methods are built-in functions that allow us to perform various operations on lists, such as adding, removing, or modifying elements. In this article, weâll explore all Python list methods with a simple example. List MethodsLet's look at different list methods in Python: append(): Adds a
3 min read
Python bytes() method
bytes() method in Python is used to create a sequence of bytes. In this article, we will check How bytes() methods works in Python. [GFGTABS] Python a = "geeks" # UTF-8 encoding is used b = bytes(a, 'utf-8') print(b) [/GFGTABS]Outputb'geeks' Table of Content bytes() Method SyntaxUs
3 min read
Python | os.DirEntry.is_file() method
OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.scandir() method of os module yields os.DirEntry objects corresponding to the
3 min read
Python - __lt__ magic method
Python __lt__ magic method is one magic method that is used to define or implement the functionality of the less than operator "<" , it returns a boolean value according to the condition i.e. it returns true if a<b where a and b are the objects of the class. Python __lt__ magic method Syntax S
2 min read
Python List append() Method
append() method in Python is used to add a single item to the end of list. This method modifies the original list and does not return a new list. Let's look at an example to better understand this. [GFGTABS] Python a = [2, 5, 6, 7] # Use append() to add the element 8 to the end of the list a.append(
3 min read
Python | os.fchdir() method
OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.fchdir() method in Python is used to change the current working directory to t
3 min read
Open a File in Python
Python provides built-in functions for creating, writing, and reading files. Two types of files can be handled in Python, normal text files and binary files (written in binary language, 0s, and 1s). Text files: In this type of file, each line of text is terminated with a special character called EOL
6 min read
Python File truncate() Method
Prerequisites: File Objects in Python Reading and Writing to files in Python Truncate() method truncate the fileâs size. If the optional size argument is present, the file is truncated to (at most) that size. The size defaults to the current position. The current file position is not changed. Note t
2 min read
Matplotlib.figure.Figure() in Python
Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot
2 min read