【发布时间】:2020-09-12 00:05:08
【问题描述】:
我的本地目录中有 10 个文本文件,命名为 test1、test2、test3 等。我想读取所有这些文件,在文件中搜索几个字符串,用其他字符串替换它们,最后以 newtest1、newtest2 之类的方式保存回我的目录em>、newtest3 等等。
例如,如果只有一个文件,我会执行以下操作:
#Read the file
with open('H:\\Yugeen\\TestFiles\\test1.txt', 'r') as file :
filedata = file.read()
#Replace the target string
filedata = filedata.replace('32-83 Days', '32-60 Days')
#write the file out again
with open('H:\\Yugeen\\TestFiles\\newtest1.txt', 'w') as file:
file.write(filedata)
有没有什么方法可以在 python 中实现这一点?
【问题讨论】:
标签: python-3.x file jupyter-notebook file-writing file-read