flush python
文件flush()方法 (File flush() Method)
flush() method is an inbuilt method in Python, it is used to clear/flush the internal buffer, it is best practice while working with fila handling in Python, the internal buffer can be cleared before writing/appending the new text to the file.
flush()方法是Python中的内置方法,用于清除/刷新内部缓冲区,这是在Python中处理fila处理时的最佳实践,可以在将新文本写入/添加到文件之前清除内部缓冲区。
Syntax:
句法:
file_object.flush()
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of this method is <class 'NoneType'>, it returns nothing.
此方法的返回类型为<class'NoneType'> ,它什么也不返回。
Example:
例:
# Python File flush() Method with Example
# creating a file
myfile = open("hello.txt", "w")
# writing text to the file
myfile.write("Hello friends, how are you?")
# flushing the internal buffer
myfile.flush()
# writing the text again
myfile.write("\nI am good, what about you?")
# flushing the internal buffer
myfile.flush()
# writing the text again
myfile.write("\nI am good too, thanks!")
# closing the file
myfile.close()
# reading content from the file
myfile = open("hello.txt", "r")
print("file content...")
print(myfile.read())
myfile.close()
Output
输出量
file content...
Hello friends, how are you?I am good, what about you?
I am good too, thanks!
翻译自: https://www.includehelp.com/python/file-flush-method-with-example.aspx
flush python