[Python]批量重命名,修改文件名中的部分字符串

```Python
#202506113 update
import os root_dir = r"D:\Dir" old = "https://www.google.com/" new = "https://m.google.com/" for root, _, files in os.walk(root_dir): for file in files: if file.endswith('.xml'): full_path = os.path.join(root, file) with open(full_path, 'r', encoding='utf-8') as f: content = f.read() if old in content: content = content.replace(old, new) with open(full_path, 'w', encoding='utf-8') as f: f.write(content) print(f"✅ 替换完成: {full_path}") else: print(f"⏩ 未发现目标字符串: {full_path}") ```

 


 

 

上一版PowerShell版本的莫名其妙的有些改不了:https://www.cnblogs.com/alfredsun/p/10124230.html

 

 

 

更新一个Python版本:

import os
files = os.listdir(os.getcwd())
for file in files:
    os.rename(file,file.replace('FLAC','FLACx2'))

 

效果:

 

posted @ 2020-01-10 22:21  夜歌乘年少  阅读(1914)  评论(0)    收藏  举报