import os import shutil def organize_files_by_prefix(folder_path): # 遍历指定文件夹 for filename in os.listdir(folder_path): # 检查是否为文件 if os.path.isfile(os.path.join(folder_path, filename)): # 按照"_"分割文件名 parts = filename.split('_') if len(parts) > 1: # 使用分割后的第一项作为新文件夹的名称 new_folder_name = parts[0] new_folder_path = os.path.join(folder_path, new_folder_name) # 如果新文件夹不存在,则创建它 if not os.path.exists(new_folder_path): os.makedirs(new_folder_path) # 移动文件到新文件夹 shutil.move(os.path.join(folder_path, filename), new_folder_path) # 指定需要整理的文件夹路径 folder_to_organize = r'E:\download\fabric' # 替换为你的文件夹路径 organize_files_by_prefix(folder_to_organize)