python:获取ValueError:关闭文件上的I/O操作

奥西法

我的代码有问题。我正在尝试替换文件中的模式。首先我在打开文件的数量上有错误,因为我忘记关闭我的文件。但是现在,我在我的代码中加入了 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函数打开太多文件时遇到问题,我尝试编写一些尽可能少地打开文件的内容。而且,你说你要编辑的文件很大,所以我避免直接​​读到内存中。

#!/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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从csv文件中获取数据:关闭文件上的IO操作错误

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

凯拉斯。ValueError:在关闭的文件上的I / O操作

openpyxl:ValueError:在关闭的文件上的I / O操作

python:ValueError:对已关闭文件的I / O操作

ValueError:对关闭的文件进行I / O操作

ValueError:对关闭的文件进行I / O操作(不应关闭文件)

CSV阅读器对象获取ValueError:对已关闭文件的I / O操作?

ValueError:关闭文件上的 I/O 操作(本地机器正常,但不是 Google Colab)

Python ValueError:对关闭的文件进行I / O操作,文件保存且无数据

Python ValueError:对关闭的文件进行I / O操作。示例教程不起作用

ValueError:打开文件后对关闭的文件进行I / O操作

flask ValueError:对已关闭文件的I / O操作

ValueError:已关闭文件的 I/O 操作。-- For 循环

我的输出显示值错误:已关闭文件上的 I/O 操作。Python

为什么相同的打开和写入文件的方式第二次出现错误?ValueError:在关闭的文件上的I / O操作

ValueError:对关闭的文件进行I / O操作(使用两个txt文件)

多线程使我收到“ ValueError:对已关闭文件的I / O操作”错误。为什么?

下载数据集时出现此错误:ValueError:对已关闭文件的I / O操作

ValueError:上下文管理器范围内对已关闭文件的I / O操作

使用pandas.read_csv()与csv.reader()冲突-ValueError:对已关闭文件的I / O操作

ValueError:针对dblp数据集的关闭文件错误的I / O操作

Python Socket Makefile错误对关闭文件的I / O操作

将文件实例作为celery任务的参数传递会引发“ ValueError:对已关闭文件的I / O操作”

在 python 上绘制带有颜色的特征时获取`ValueError`

为什么我的文件关闭并导致第 51 行出现“已关闭文件上的 I/O 操作”错误?

ValueError:即使在为open()提供第二个arg之后,仍对关闭的文件进行I / O操作

尝试从相对路径读取文件时,获取“ java.io.IOException:流已关闭”

ValueError: 寻找关闭的文件 | pdf水管工