Python文件flush()方法

Python文件flush()方法用于刷新内部缓冲区,就像stdiofflush一样。关闭它们时,Python会自动刷新文件。但有时您可能希望在关闭任何文件之前就要刷新数据。

语法

以下是flush()方法的语法 -

fileObject.flush()

参数

  • NA

返回值

  • 此方法不返回任何值。

示例

以下示例显示了flush()方法的用法。

#!/usr/bin/python3

# 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 read operation.
fo.flush()

# Close opend file
fo.close()

执行上面代码后,将得到以下结果 -

Name of the file:  foo.txt

上一篇: Python文件对象方法 下一篇: Python os模块方法