无涯教程-File函数 - file.flush()函数

192 阅读1分钟

Python file方法flush()刷新内部缓冲区,就像stdio的fflush一样。这可能是对某些类似文件的对象的无操作。

Python会在关闭文件时自动刷新这些文件。但您可能希望在关闭任何文件之前刷新数据。

file.flush() - 语法

fileObject.flush(); 

file.flush() - 示例

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

#!/usr/bin/python

# 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

参考链接

www.learnfk.com/python/file…