批量移动文件夹到对应文件夹

import os
import shutil
import copy
import pathlib

source_path = os.path.abspath(r’G:\ruijin\AD128~’)

source_path = os.path.abspath(r’C:\Users\86191\Desktop\beifen’)

target_path = os.path.abspath(r’E:\ruijinbold’)

filelist = os.listdir(source_path);

for files in filelist:
final_source_path = os.path.join(source_path, files) # 源文件
final_target_path = os.path.join(target_path, files) # 目标文件

if os.path.exists(final_target_path):
    # 如果目标路径存在原文件夹的话就先删除
    shutil.rmtree(final_target_path)
if not os.path.exists(final_target_path):
    # 如果目标路径不存在原文件夹的话就创建
    os.makedirs(final_target_path)

key1 = 'BOLD'
key2 = 'MPRAGE_AV'
for file in os.listdir(final_source_path):
    # is file
    # if os.path.isfile(file1 + '/' + file):
        if key1 in file:
            print('找到包含"' + key1 + '"字符的文件,绝对路径为----->' + final_source_path + '\\' + file)
            print('复制到----->' + final_target_path + '\\' + file)
            shutil.copytree(final_source_path + '\\' + file, final_target_path + '\\' + file)
        if key2 in file:
            print('找到包含"' + key2 + '"字符的文件,绝对路径为----->' + final_source_path + '\\' + file)
            print('复制到----->' + final_target_path + '\\' + file)
            shutil.copytree(final_source_path + '\\' + file, final_target_path + '\\' + file)


print('copy dir finished!')
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值