flush python_带有示例的Python File flush()方法

Python的flush()方法用于清除文件处理时的内部缓冲区,确保数据及时写入文件。该方法无需参数,返回值为NoneType。示例展示了如何使用flush()确保文件内容正确写入。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值