python file buffering

Insert picture description here

Insert picture description here
Observation buffer

import time
def main():
    # w代表输出 文件不存在时,自动创建,创建成功之后写入数据
    file =  open("E:\\test.txt", mode="w",encoding="UTF-8",buffering=1);
    file.write("奥特曼打小怪兽");
    # 刷新缓冲
    # file.write("\n");
    # 休眠10秒,观察数据在缓冲中还未写入到文件
    time.sleep(10);
if __name__ == '__main__':
    main();

Use flush to force the buffer to be flushed

def main():
    # w代表输出 文件不存在时,自动创建,创建成功之后写入数据
    file =  open("E:\\test.txt", mode="w",encoding="UTF-8",buffering=1);
    file.write("奥特曼打小怪兽");
    # 刷新缓冲
    file.flush()
if __name__ == '__main__':
    main();

Whenever the file stream is closed using the close() method, the flush() method is also called by default to clear the buffer and mandatory output

Guess you like

Origin blog.csdn.net/weixin_44887276/article/details/114919267