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

复制代码
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 @   Arxu  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示