关闭文件上的I / O操作错误

时间:2017-08-25 17:27:08

标签: python

有谁能告诉我这段代码有什么问题。

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)

错误是关闭文件的I / O操作 writer.writerow(行) ValueError:关闭文件的I / O操作

1 个答案:

答案 0 :(得分:-1)

你应该缩进一切:

with open("1.csv") as sample:
    ....
    with open("out1.csv", "wb") as out1:
        ....
        for path in glob.glob("out.csv"):
            ....