给一个列表,一个文件,复制文件重命名到指定文件

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.")

 

posted @ 2023-11-19 14:17  Arxu  阅读(16)  评论(0)    收藏  举报