Python file flush() method

Python File flush() Method

The flush() method flushes the internal buffer, similar to stdio’s fflush. For some file-like objects, this may be a no-op.

Python automatically flushes files when they are closed. However, you may want to flush the data before closing any file.

Syntax

The syntax of the flush() method is as follows −

fileObject.flush()

Parameters

NA

Return Value

This method does not return any value.

Example

The following example demonstrates the use of the flush() method.

# Open a file
fo = open("foo.txt", "wb")
print ("Name of the file: ", fo.name)
# Here it does nothing, but you can call it with a read operation.
fo.flush()

# Close the opened file
fo.close()

When we run the above program, it produces the following output −

Name of the file: foo.txt

Leave a Reply

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