0
任何人都可以告訴我這段代碼有什麼問題。關閉文件的I/O操作錯誤
import csv
import glob
with open("1.csv") as sample:
reader = csv.reader(sample)
header = 'Name', 'ID', 'Marks'
with open("out1.csv", "wb") as out1:
writer = csv.writer(out1)
writer.writerow(header)
for path in glob.glob("out.csv"):
if path == "out1.csv": continue
with open(path) as fh:
reader = csv.reader(fh)
for row in reader:
if row[0] == 'Name' and row[1] == 'ID':
writer.writerow(row)
錯誤是在關閉的文件 writer.writerow(行) ValueError異常I/O操作:I/O操作上關閉的文件
你能請把錯誤信息? –
從'writer.writerow(row)'?您使用上下文管理器「with」打開文件,這意味着只要您退出該縮進塊,該文件就會自動關閉。 – roganjosh
是的,你關閉了你傳遞給'writer'的文件處理程序。 「with」塊的全部要點是*保證*一旦你退出塊,文件將被關閉...... –