Python文件操作获取错误:ValueError:关闭文件上的I / O操作

时间:2017-01-27 18:24:57

标签: python csv

我是python中的新手。我正在尝试将数据存储在viewDidLoad()文件中,但是当我运行程序时,我收到以下错误:

.csv

我的代码在这里:

Name: Msc Id: A100
Traceback (most recent call last):
  File "./python.py", line 29, in <module>
    obj.writeInCsvFile()
  File "./python.py", line 24, in writeInCsvFile
    fp.write(column)    
ValueError: I/O operation on closed file

有人帮助我。提前谢谢。

1 个答案:

答案 0 :(得分:3)

您的for循环未缩进with块内。像这样缩进它们:

    def writeInCsvFile(self):
            with open("file.csv", "wb") as fp:
                    wr = csv.writer(fp)

                    data = [["Msc", "Ijp"], ["10", "20"], ["50", "60"]]

                    for row in data:
                            for column in row:
                                    fp.write(column)
                                    fp.write("\t")