本示例代码可遍历多层文件目录,批量替换文件名、文件夹名内字符串。
要点:文件和文件夹分开替换,先统一替换完文件再单独替换文件夹。
1、通过os.listdir()返回根目录文件内所有文件和文件夹名字的目录,isdir()判断是否是文件夹,根据判断结果进行进一步处理。
2、replace()函数完成字符串替换。
3、os.rename() 函数重命名文件或文件夹,重命名前需判断是否存在新文件名或者通过抛出异常的方式进行重命名。
注:同一目录中已存在替换后同名文件时会重命名失败
import os
#批量替换文件名
def change_file(target_path):
for file_name in os.listdir(target_path):
file_path = os.path.join(target_path,file_name)
#判断路径是否为文件
if os.path.isdir(file_path):
change_file(file_path)
else:
new_file_name = file_name.replace(old_str, new_str)
#法一:比对新旧文件名,不一致则重命名文件
if new_file_name != file_name:
os.rename(file_path, os.path.join(target_path, new_file_name))
print('旧文件'+file_name+'已重命名为:'+new_file_name)
#批量替换文件夹名
def change_folder(target_path):
for file_name in os.listdir(target_path):
file_path = os.path.join(target_path,file_name)
if os.path.isdir(file_path):
new_folder_name = file_path.replace(old_str,new_str)
#法二:直接重命名,命名失败则抛出异常
try:
os.rename(file_path,new_folder_name)
print(f'{file_path} 已重命名为: {new_folder_name}')
change_folder(new_folder_name)
except Exception as e:
print('文件夹'+file_path+'重命名失败。报错:'+ e)
# 根目录
target_path = r"根目录"
#待替换字符串
old_str = "待替换字符串"
#新字符串
new_str = "新字符串"
change_file(target_path)
change_folder(target_path)