| import os |
|
|
| def rename_items_recursive(root_path): |
| |
| |
| subdirectories = [] |
| for item in os.listdir(root_path): |
| item_path = os.path.join(root_path, item) |
| if os.path.isdir(item_path): |
| subdirectories.append(item) |
| |
| |
| for subdir in subdirectories: |
| subdir_path = os.path.join(root_path, subdir) |
| rename_items_recursive(subdir_path) |
| |
| |
| for item in os.listdir(root_path): |
| old_path = os.path.join(root_path, item) |
| |
| |
| if "wooden" in item: |
| |
| new_name = item.replace("wooden", "yellow") |
| new_path = os.path.join(root_path, new_name) |
| |
| |
| if old_path != new_path: |
| os.rename(old_path, new_path) |
| print(f"已重命名: {old_path} -> {new_path}") |
|
|
| if __name__ == "__main__": |
| |
| root_folder = "./yellow_cabinet" |
| |
| |
| if not os.path.exists(root_folder): |
| print(f"错误:根文件夹 '{root_folder}' 不存在") |
| else: |
| |
| rename_items_recursive(root_folder) |
| print("所有重命名操作完成") |
|
|