Facebook Page Twitter Page LinkedIn Page
× Python Tutorial Python Advanced Python Resources Python Libraries


The Python file flush() method is used to flush out the internal buffer. When a file is closed, Python automatically flushes out the internal buffer. This method is useful when the flushing of the internal buffer is needed while the file is open. Please note that this method may be a no-op on some file-like objects.

Syntax

file.flush()

Parameters

No parameter is required.

Return Value

None.

Example: Flush internal buffer

In the below example, Python file flush() method is used to flush the internal buffer.

MyFile = open("python_test.txt", "r")

#read content before closing the file
print("Name of the file:", MyFile.name)
print(MyFile.read())

#flushing the internal buffer
MyFile.flush()

#closing the open file
MyFile.close()

The output of the above code will be:

Name of the file: python_test.txt
Python is a programming language.
Learning Python is fun.

❮ Python File Handling Methods