import os import shutil data = """E670Z""" # 按照 "E" 进行分割,并保留 "E" 在每个元素前面 elements = ["E" + item.strip() for item in data.split("E") if item] print(elements) # 源文件路径 source_file_path = r"D:\cadZZ.py" # 遍历元素列表,复制文件 for element in elements: # 目标文件夹路径 target_folder_path = os.path.join(r"D:\cadquereD", element) # 创建目标文件夹 if not os.path.exists(target_folder_path): os.makedirs(target_folder_path) target_file_path = os.path.join(target_folder_path, f"{element}.py") # 检查源文件和目标文件是否相同,避免 SameFileError if source_file_path != target_file_path: shutil.copyfile(source_file_path, target_file_path) print(f"File '{element}.py' copied to '{target_folder_path}'") else: print(f"Skipping copy for '{element}.py' as source and target paths are the same.")
标签:重命名,文件,target,列表,file,path,folder,element,os From: https://www.cnblogs.com/arwen-xu/p/17842000.html