python: getting ValueError: I/O operation on closed file
python: getting ValueError: I/O operation on closed file
我的代码有问题。我正在尝试替换文件中的模式。首先,我在打开文件的数量上有一个错误,因为我忘记关闭我的文件。
但是现在,我的代码中有 f.close() 并且出现以下错误:
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
函数编辑成类似的东西,它应该可以工作(如果不能,请评论错误):
def main():
"""
Replace all year-month-days which have a possible wrong pattern
"""
occurrences = 999 # arbitray number. If you know the exact number, you may want to edit this.
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, count=occurrences)
我将此作为另一个答案发布,因为它明显不同。
由于您的 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" "
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()
我的代码有问题。我正在尝试替换文件中的模式。首先,我在打开文件的数量上有一个错误,因为我忘记关闭我的文件。 但是现在,我的代码中有 f.close() 并且出现以下错误:
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
函数编辑成类似的东西,它应该可以工作(如果不能,请评论错误):
def main():
"""
Replace all year-month-days which have a possible wrong pattern
"""
occurrences = 999 # arbitray number. If you know the exact number, you may want to edit this.
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, count=occurrences)
我将此作为另一个答案发布,因为它明显不同。
由于您的 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" "
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()