代码十几秒的事,为什么要手动去改一天呢!直接放源码~
重命名指定目录下所有文件夹
import os
import redef extract_first_three_numbers_from_folder_name(folder_name):numbers = re.findall(r'\d+', folder_name)return numbers[:3]def rename_folders_with_prefixes(directory):# 获取目标目录下的所有文件夹folder_names = [name for name in os.listdir(directory) if os.path.isdir(os.path.join(directory, name))]for folder_name in folder_names:# 提取文件夹名称中的前三个数字部分numbers = extract_first_three_numbers_from_folder_name(folder_name)# 只有在数字数量满足条件时才进行重命名if len(numbers) == 3:# 在每个数字前添加相应的字符prefixed_numbers = [f'V{numbers[0]}', f'D{numbers[1]}', f'Q{numbers[2]}']# 构建新的文件夹名称new_folder_name = "_".join(prefixed_numbers)# 构建完整的路径old_path = os.path.join(directory, folder_name)