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模組方法