Enable Dark Mode!
By: Nihala MK

How to Use Flush & Close Function in Python File Handling

flush() Method

The flush() method compels the internal buffer to immediately write its contents to the actual file, ensuring instant data preservation. It proves valuable when you need to ensure that the data you've written is securely saved without delay, even prior to closing the file. This function allows you to swiftly transfer data from Python's temporary memory to a file without terminating the file. This practice is advantageous for various scenarios, such as promptly accessing files after processing. It's advisable not to retain excessive data in Python's memory.
When writing to a file, data is initially stored in an internal buffer before being transferred to the actual file. This buffering minimizes system calls for each write operation. Python automatically flushes this buffer when the file is closed, but there may be situations where you want to flush the data manually before closing the file.


When you use flush(), it doesn't clean out the original file, it only clears what's in the buffer inside.

Basic flush() Usage

# Open a file in write mode
with open("textfile.txt","w") as f:
    # Write this content to the file
    f.write("This is an example of flush()")
    f.flush()  #flush buffer to ensure immediate write 

Using Flush() in a loop

with open('example.txt','a') as file:
For i in range(3):
file.write(f'line (i +1)\n')

close() Method

In Python, the close() method shuts down the file that's currently open. It's widely understood that when a file is opened for a specific purpose, it should be closed once that purpose is fulfilled. This helps prevent the operating system from having too many open files, which could exceed its set limit. 

After a file is closed, it becomes inaccessible for reading or writing. Attempting any operation on a closed file will result in a ValueError because the file must be open for any action to take place.




The close() method can be called multiple times in a single program.

 # Open a file using object 'fo'
Fo = open('example.txt','w')
# After closing the file it perform an operation
fo.write("Hello World")
# Close the opened file

After running the program mentioned earlier, the text written using the write() method appears in the example.txt file as depicted below.

Hello World

Note:  Once the file is closed, it's not possible to carry out any operations on it. Trying to do so will result in a ValueError being raised. For example,

# Open a file using file object 'fo'
fo = open('example.txt', 'w')
# close the opened file
# perform write operation after closing the file
fo.write("Hello World")

We'll compile and execute the provided program to generate the following output:

Traceback (most recent call last):
  File "main.py", line 8, in <module>
fo.write("Hello World")
ValueError: I/O operation on closed file.

In conclusion, the flush() function in Python ensures the immediate writing of data from the internal buffer to the file, facilitating prompt data persistence. This is particularly useful when you need to guarantee that data is saved without delay, even before closing the file. On the other hand, the close() function terminates the file, making it inaccessible for further reading or writing. It's crucial to close files once their purpose is fulfilled to prevent resource wastage and potential system errors.

To read more about How to Setup Odoo 17 Development Environment Using Pycharm in Ubuntu 20.04, refer to our blog How to Setup Odoo 17 Development Environment Using Pycharm in Ubuntu 20.04

If you need any assistance in odoo, we are online, please chat with us.


Leave a comment




Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message