python:获取值错误:对关闭的文件执行I/O操作
我的代码有问题。我正在尝试替换文件中的模式。首先,我在打开文件的数量上有一个错误,因为我忘记关闭我的文件。 但是现在,我在代码中加入了f.close(),出现了以下错误:python:获取值错误:对关闭的文件执行I/O操作,python,valueerror,fclose,Python,Valueerror,Fclose,我的代码有问题。我正在尝试替换文件中的模式。首先,我在打开文件的数量上有一个错误,因为我忘记关闭我的文件。 但是现在,我在代码中加入了f.close(),出现了以下错误: ValueError: I/O operation on closed file. 在这里你可以找到我的代码的一部分。有人知道怎么回事吗 #!/usr/bin/env python # -*- coding: utf-8 -*- import re import shutil from tempfile import mk
ValueError: I/O operation on closed file.
在这里你可以找到我的代码的一部分。有人知道怎么回事吗
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import shutil
from tempfile import mkstemp
infile = 'test_file.txt'
year = int('2009')
month = int('1')
day = int('10')
#################################################################################
def sed(pattern, replace, source):
"""
Reads a source file and writes the destination file.
In each line, replaces pattern with replace.
Args:
pattern (str): pattern to match (can be re.pattern)
replace (str): replacement str
source (str): input filename
"""
fin = open(source, 'r')
fd, name = mkstemp()
fout = open(name, 'w')
for line in fin:
out = re.sub(pattern, replace, line)
fout.write(out)
fin.close()
fout.close()
shutil.move(name, source)
#################################################################################
def main():
"""
Replace all year-month-days which have a possible wrong pattern
"""
for i in range (6):
for j in range (12):
for k in range (22):
Year = year + i; Month = month + j; Day = day + k
pattern = '%s %s%s' %(Year, Month, Day)
replace = '%s %s %s' %(Year, Month, Day)
sed(pattern, replace, infile)
#################################################################################
if __name__ == "__main__":
main()
###### END
多谢各位 我认为问题在于您错误地使用了
sed
功能。在main
函数中打开要编辑的文件,然后在sed
函数中再次打开(然后关闭)
看起来您的sed
函数应该用于整个文件,而不仅仅是一行
如果您将main
函数编辑为类似的内容,它应该可以工作(如果不行,请说明错误):
我将此作为另一个答案发布,因为它显然不同 由于您的
sed
函数打开的文件太多,因此我尝试编写一些尽可能少打开文件的内容。此外,您说您要编辑的文件相当大,因此我避免将其直接读入内存
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
input_file = 'test_file.txt'
output_file = 'result_file.txt'
def main():
pattern = r"(\d{4}) (\d{1,2})(\d{2})"
replace = r"\1 \2 \3"
with open(input_file, "r") as inp:
with open(output_file, "w") as oup:
for line in inp:
sub = re.sub(pattern, replace, line)
oup.write(sub)
if __name__ == "__main__":
main()
如果您改用上下文管理器会发生什么情况?您确定原始程序中没有缩进错误吗?例如:将open(infle)设为f?没有缩进错误(我在编辑器中使用的是不可见),您可以编辑您的帖子并向我们展示您的
sed
函数吗?好的,谢谢,这很有道理。你能解释一下你为什么要删除i、j和k上的3个循环吗?@OcéF对不起,我以为这些循环是用来在行上循环的,所以我删除了它们,但我重新阅读了你的代码,理解了它们的功能。我将编辑代码。谢谢。我不知道发生的次数。这是一个大文件。但它不起作用。。。我仍然有同样的问题:OSError:[Errno 24]打开的文件太多:@OcéF wird。似乎sed
函数应该关闭它正在打开的所有文件。顺便问一下,您的脚本中是否还有另一部分正在打开文件?您可能需要检查infle
是如何声明的,或者main
函数是如何调用的。在“最坏”的情况下,你必须重新编写你的sed
^^^^这真的很奇怪。我不明白。我在别的地方不叫它。我改变了sed函数,试图使其简单化,但仍然存在同样的问题。我再次编辑了我的代码。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
input_file = 'test_file.txt'
output_file = 'result_file.txt'
def main():
pattern = r"(\d{4}) (\d{1,2})(\d{2})"
replace = r"\1 \2 \3"
with open(input_file, "r") as inp:
with open(output_file, "w") as oup:
for line in inp:
sub = re.sub(pattern, replace, line)
oup.write(sub)
if __name__ == "__main__":
main()