我有一些大的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
是否应该始终在(文件)上下文管理器的末尾使用?
答案 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
>>之外文件现已刷新并关闭