【问题标题】:What's Wrong With My Code? (ValueError: I/O operation on closed file)我的代码有什么问题? (ValueError:对已关闭文件的 I/O 操作)
【发布时间】:2013-05-03 12:10:21
【问题描述】:

有人可以向我解释我的代码有什么问题吗?我收到以下错误:

Traceback(最近一次调用最后一次):
文件“C:\LineRep.py”,行 15、在模块中:
对于 File2 中的行:
ValueError: I/O operation on closed file

我的代码:

import os, Tkinter, tkFileDialog
root = Tkinter.Tk()
root.withdraw()
dirprompt = tkFileDialog.askopenfilename()

File = open (dirprompt, 'r')
File2 = open (dirprompt + 'temp', 'w')
for line in File:
    File2.write(line.replace(',', ' '))
File.close()
File2.close()

names = []
for line in File2:
    names.append(line)
print names

【问题讨论】:

    标签: python


    【解决方案1】:

    在调用 File2.close() 后,您正在对 File2 进行迭代

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-27
      • 2016-07-21
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 2016-08-26
      • 2016-06-13
      相关资源
      最近更新 更多