[英]Regex Search Replace for Multiple Files in a Directory using Python
Where am I going wrong ?? 我要去哪里错了?
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)
Somehow its not working. 不知何故,它不起作用。 I want to make the replacements in the current file and not a new file. 我想在当前文件而不是新文件中进行替换。
Update: How about creating A_reg.java from A.java. 更新:如何从A.java创建A_reg.java。 Moving A.java to a separate local folder and then renaming A_reg.java back to A.java . 将A.java移动到单独的本地文件夹,然后将A_reg.java重命名回A.java。 Possible ? 有可能吗 If yes, please help me out with the code. 如果是,请帮助我提供代码。
This is perfectly normal: you overwrite the files themselves. 这完全正常:您覆盖文件本身。 Write in new files, then rename. 写入新文件,然后重命名。
Also, opening the way you do truncates files: 另外,打开截断文件的方式:
$ cat t.txt
foo
$ python
>>> f = open("t.txt", "w")
>>> f.close()
>>> exit()
$ cat t.txt
# file is empty!!
$
Based on the inputs from Fge. 基于Fge的输入。 I could make it work by using 我可以通过使用使其工作
from shutil import move
move(output_file, input_file)
Thus the working code will be 因此, 工作代码将是
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.