import os # 设置你的根路径(注意:路径中的反斜杠在 Python 字符串中需转义,或使用 raw string) root_path = r"C:\Users\hp_z66\OneDrive\Desktop\新建文件夹\合作跟进_20260207110728_resources_4" # 遍历根路径下的所有文件 for dirpath, dirnames, filenames in os.walk(root_path): # 判断当前路径是否包含 "FINST-" 开头的父目录 # 我们假设 FINST 目录是 root_path 的直接子目录 rel_path = os.path.relpath(dirpath, root_path) parts = rel_path.split(os.sep) if parts[0].startswith("FINST-"): finst_id = parts[0] for filename in filenames: old_file_path = os.path.join(dirpath, filename) _, ext = os.path.splitext(filename) # 获取原扩展名 new_filename = finst_id + ext new_file_path = os.path.join(dirpath, new_filename) # 防止覆盖同名文件(可选) if old_file_path != new_file_path: os.rename(old_file_path, new_file_path) print(f"Renamed: {old_file_path} → {new_file_path}")