删除多个文件名中相同的指定字段
删除多个文件名中相同的指定字段
删除"HC1201150"字段:
import os def delete_files_with_string(directory, substring): for root, _, files in os.walk(directory): for file in files: if substring in file: full_path = os.path.join(root, file) new_path = full_path.replace(substring, '') # 构建新路径,移除特定字符串 os.rename(full_path, new_path) # 重命名文件,删除特定字符串部分 print(f"Renamed: {full_path} -> {new_path}") # 指定要删除的文件夹路径 folder_to_delete = "/Users/admin/Desktop/new/" # 调用函数在指定目录及其所有子目录中处理文件名 delete_files_with_string(folder_to_delete, "HC1201150")