繁体   English   中英

正则表达式搜索使用Python替换目录中的多个文件

[英]Regex Search Replace for Multiple Files in a Directory using Python

我要去哪里错了?

import os, os.path, re

path = "D:\python-test"
myfiles = os.listdir(path)

REGEXES = [(re.compile(r'dog'), 'cat'),
           (re.compile(r'123'), '789')]
for f in myfiles:

    file_name, file_extension = os.path.splitext(f)

    if file_extension in ('.txt', '.doc', '.odt', '.htm', '.html', '.java'):

        input_file = os.path.join(path, f)

        with open(input_file, "w") as fi:
            for line in fi:
                for search, replace in REGEXES:
                    line = search.sub(replace, line)
                fi.write(line)

不知何故,它不起作用。 我想在当前文件而不是新文件中进行替换。

更新:如何从A.java创建A_reg.java。 将A.java移动到单独的本地文件夹,然后将A_reg.java重命名回A.java。 有可能吗 如果是,请帮助我提供代码。

这完全正常:您覆盖文件本身。 写入文件,然后重命名。

另外,打开截断文件的方式:

$ cat t.txt 
foo
$ python
>>> f = open("t.txt", "w")
>>> f.close()
>>> exit()
$ cat t.txt
# file is empty!!
$ 

基于Fge的输入。 我可以通过使用使其工作

from shutil import move

move(output_file, input_file)

因此, 工作代码将是

import os, os.path, re
from shutil import move

path = "D:\python-test"
myfiles = os.listdir(path)

REGEXES = [(re.compile(r'dog'), 'cat'),
           (re.compile(r'123'), '789')]
for f in myfiles:

file_name, file_extension = os.path.splitext(f)
generated_output_file = file_name + "_regex" + file_extension

if file_extension in ('.txt', '.doc', '.odt', '.htm', '.html', '.java'):

    input_file = os.path.join(path, f)
    output_file = os.path.join(path, generated_output_file)

    with open(input_file, "r") as fi, open(output_file, "w") as fo:
        for line in fi:
            for search, replace in REGEXES:
                line = search.sub(replace, line)
            fo.write(line)

move(output_file, input_file)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM