import os;
def myrepl(dir, oldStr, newStr):
d = os.walk(dir)
for path, dirList, fileList in d:
for fileName in fileList:
oldFile = os.path.join(path, fileName)
if oldStr in oldFile:
print(oldFile)
newFile = os.path.join(path, fileName.replace(oldStr, newStr))
if os.path.isfile(oldFile):
os.rename(os.path.join(path, fileName), os.path.join(path, fileName.replace(oldStr, newStr)))
print(newFile)
if __name__ == '__main__':
while True:
fileDir = input('请输入文件路径:')
#fileDir = r"C:\Users\y\Documents\WeChat Files\wxid_lk6oeqnjj3a721\FileStorage\File\2022-03\至尊宝下载评论-21个_\至尊宝下载评论-21个" # 目标根目录
oldStr = '内容' # 要替换的字符串
newStr = '评价文字'
myrepl(fileDir, oldStr, newStr)
print("处理成功")
|