Python3 File flush() Method
Overview
flush()This method is used to flush the buffer, i.e., to immediately write the data in the buffer to the file and clear the buffer, without having to passively wait for the output buffer to be written.
Generally, the buffer is automatically flushed after the file is closed, but sometimes you need to flush it before closing, in which case you can use the flush() method.
Syntax
The syntax of the flush() method is as follows:
fileObject.flush();
Parameters
None
Return Value
This method has no return value.
Example
The following example demonstrates the use of the flush() method:
#!/usr/bin/python3
# 打开文件
fo = open("example.txt", "wb")
print ("文件名为: ", fo.name)
# 刷新缓冲区
fo.flush()
# 关闭文件
fo.close()
The output of the above example is:
文件名为: example.txt
Other Extensions
Python3 File Methods