文件复制到目标地址-保持tree结构不变

import os
import shutil
target_path = r"J:\ILSVRC2015\ILSVRC2015"
source_path = r"J:\ILSVRC2015_data\16-22\ILSVRC2015"

for root, dirs, files in os.walk(source_path):
        for file in files:
            src_file = os.path.join(root, file)
            sub_file = root.lstrip(source_path)  # 获取子路径
            target_dir = os.path.join(target_path, sub_file)  # 创建目标目录路径
            if not os.path.exists(target_dir):
                os.mkdir(target_dir)
            target_file = os.path.join(target_path, sub_file, file)  # 获取目标文件位置路径
            shutil.copy(src_file, target_file)      # 复制过去
        print(root)

print("Well done".center(40, '*'))

  

posted @ 2020-04-27 10:08  SaltFish-T  阅读(98)  评论(0)    收藏  举报