Python3 File flush() Method
Overview
flush() The method flushes the buffer, i.e. writes the buffered data to the file immediately and clears the buffer, without passively waiting for the output buffer to be written.
Normally, closing a file flushes the buffer automatically, but sometimes you need to flush it before closing, in which case you can use flush().
Syntax
The flush() method syntax is as follows:
fileObject.flush();
Parameters
-
None
Return Value
This method has no return value.
Example
The following example shows how to use the flush() method:
#!/usr/bin/python3
# Open file
fo = open("runoob.txt", "wb")
print ("文件名为: ", fo.name)
# Flush buffer
fo.flush()
# Close file
fo.close()
The output of the above example is:
文件名为: runoob.txt
Python3 File methods