python - Python 搜索文件并查找和替换值
问题描述
嗨,我正在尝试编写一个 python 脚本,它将...
查找和替换文件中的某些值。问题是有多个同名文件,并非所有文件都具有这些值。
在更改值之前备份文件也很好。
到目前为止,我已经设法搜索到名为 test1.txt 的文件。输出显示了它找到 test1.txt 的多个位置接下来我需要它来搜索诸如“Scott”之类的值并将其替换为“John”,并在可能的情况下进行更改之前进行备份。
并非所有找到的 test1.txt 都具有名称“Scott”,所以我真的不想对它们进行备份。
import os
filename[]
def find_files(filename, search_path):
result = []
for root, dirs, files in os.walk(search_path):
if filename in files:
result.append(os.path.join(root, filename))
return result
print(find_files("test1.txt", '/home/')
解决方案
推荐阅读
- windows - 如果其中有“completed.txt”文件,则移动特定的子目录
- android - 无法在真实设备上运行程序
- java - 如果 sql 值更改,则更新 android 应用程序
- spring - 使用 H2 的 Java Spring 项目中的查询问题
- encryption - 通过网络在两台设备之间共享公钥 [RSA]
- python - 在进程之间共享布尔值
- c# - 如何将数据库表与excel与c#中的相同列进行比较?
- php - Opencart 控制器数据未在视图中定义
- ios - UIImagePickerController 没有选择图像
- c++ - OpenCV 中的 findContours 与 Matlab 中的 bwboundaries 不匹配?