您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> 编程语言综合 >> 正文

在Python程序中操作文件之flush()方法的使用

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   这篇文章主要介绍了在Python程序中操作文件之flush()方法的使用教程,是Python入门学习中的基础知识,需要的朋友可以参考下

  flush()方法刷新内部缓冲区,像标准输入输出的fflush。这类似文件的对象,无操作。

  Python关闭时自动刷新文件。但是可能要关闭任何文件之前刷新数据。

  语法

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

  ?

1 fileObject.flush();

  参数

  NA

  返回值

  此方法不返回任何值。

  例子

  下面的例子显示了flush()方法的使用。

  ?

1 2 3 4 5 6 7 8 9 10 11 #!/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