Using the Flush & Close function in Python File

May 22, 2024

flush() Method

It is executed by flush() to write the contents of the internal buffer to the actual file as soon as possible, Data preservation is ensured instantly. It’s useful if you want to make sure that the information you write is safely stored right away, even before you close the file. With the help of this method, you can quickly move data from Python’s temporary memory to a file without closing it. There are many situations where this practice is advantageous. For instance, after a file is processed, it can be accessed promptly. Python recommends not storing excessive amounts of data in its memory.

Data is first saved in an internal buffer when writing to a file and then moved to the actual file. Every write operation uses fewer system calls because of this buffering. Although Python automatically flushes this buffer at file closure, there may be circumstances in which you would like to manually flush the data before file closure.




Flushing() doesn’t delete the original file, it only clears the contents of 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

The current file is closed in Python using the close() function. It is common knowledge that once a file is opened with a specific intention, it should be closed. By doing this, the operating system is kept from having more open files than it is designed to have.

Putting a file on hold makes it unreadable, unwriteable, and inaccessible. If you attempt any operation on a closed file, you’ll get a ValueError, as the file must be open to be operated on.




Within a single program, the close() method may be called more than once.

 # Open a file using object ‘fo’

Fo = open(‘example.txt’,’w’)

# After closing the file it performs an operation

fo.write(“Hello World”)

# Close the opened file

fo. close()

The text created with the write() method appears in the example.txt file as seen below after executing the previously mentioned program.

Hello World

Note: After that, a closed file cannot be opened or modified. There will be a ValueError if this is tried. For example,

# Open a file using file object ‘fo’

fo = open(‘example.txt’, ‘w’)

# close the opened file

fo. close()

# perform write operation after closing the file

fo.write(“Hello World”)

After compiling and running the given program, the following output will be produced:

Traceback (most recent call last):

File "", line 8, in <module>
fo.write("Hello World")
ValueError: I/O operation on closed file.

To sum up, the Python flush() function guarantees that data is written from the internal buffer to the file immediately, allowing for timely data persistence. This is especially helpful if you need to ensure that data is saved immediately, even before the file is closed. Conversely, the close () method ends the file and prevents access to it for more writing or reading. Once a file’s function has been completed, it is imperative to close it to avoid wasting resources and perhaps causing system issues.

"Unlock the Full Potential of Your Business with Odoo ERP!"

"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"

Get a Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *