Skip to main content

flush()

The flush() method in Python is used with file objects to clear the internal buffer and write data to the file immediately without waiting for the buffer to fill up. This method is typically used to ensure that all data written to the file is immediately saved to the underlying storage.

Parameter Values

This function does not accept any parameters.

Return Values

The flush() method in Python returns None.

How to Use flush() in Python

Example 1:

The flush() method flushes the internal buffer, ensuring that all data is written to the file.

f = open('example.txt', 'w')
f.write('Hello, World!')
f.flush()
f.close()