Python的文件缓冲区没有用块刷新?

时间:2018-02-08 10:59:43

标签: python python-3.x csv buffer

我有一些大的csv文件,我打开顺序处理每一行,然后将结果写入单个csv文件,我的代码看起来像这样(我在Debian 9上使用Python 3.5顺便说一句):

with open("coutput.csv", "w") as outf:
    ## csv_file_names is a list of csv files' paths
    for file_name in csv_file_names:
        with open(file_name) as inf:
            for line in inf:
                ## processing the line's data
                ....
                ## processed data is then stored in processed_line(as an str object)
                outf.write(processed_line)

问题在于,在上一次迭代中似乎没有processed_line的{​​{1}}被写入output.csv;换句话说,output.csv的行数应该少于它应该具有的行数(不写入最后10行)。当我通过将outf.flush添加到第一个with block的末尾来修改我的代码时,每件事情都很好(没有丢失的行)。

with open("coutput.csv", "w") as outf:
    ## csv_file_names is a list of csv files' paths
    for file_name in csv_file_names:
        with open(file_name) as inf:
            for line in inf:
                ## processing the line's data
                ....
                ## processed data is then stored in processed_line(as an str object)
                outf.write(processed_line)
    ## adding flush solves the problem
    outf.flush()

所以我要问,flush是否应该始终在(文件)上下文管理器的末尾使用?

1 个答案:

答案 0 :(得分:0)

关于离开with的嵌套部分的全部内容。 随着with的结束,缓冲区被清除,文件被关闭:

with open("coutput.csv", "w") as outf:
    outf.write('something')
    outf.write('something more')

    print(1)
    # still in buffer
print(2)
# now its flushed
  • print(1)位于with>>内文件仍处于打开状态且未刷新
  • print(2)位于with>>之外文件现已刷新并关闭