复制指定文件夹的特定文件到指定文件夹内

import os
import shutil

# 复制指定文件夹的特定文件到指定文件夹内
def copyFile(beforPath, afterPath):
    if not os.path.isfile(beforPath):
        print("%s not exist!" % (beforPath))
    else:
        filePath, fileName = os.path.split(beforPath)
        if not os.path.exists(afterPath):  # 判断文件夹是否存在,不存在则创建
            os.makedirs(beforPath)
        shutil.copy(beforPath, afterPath + fileName)  # 复制文件到指定位置,若移动则为move
        print("copy %s -> %s" % (beforPath, afterPath + fileName))


before_dir = 'F:\MyData_fx_kg_procesing\KG\KG_VBM\T1\\'
after_dir = 'F:\MyData_fx_kg_procesing\KG\KG_VBM\T1_nii2\\'

for num in range(1, 41):
    if num < 10:
        i = '0' + str(num)
        before_file_dir = before_dir + 'sub' + i    # 原始文件存放路径拼接
        if os.path.exists(before_file_dir):
            dirs = os.listdir(before_file_dir)  # 获取指定路径下的文件
            for f in dirs:
                if os.path.splitext(f)[1] == '.nii':  # 筛选文件夹内的nii文件
                    before_file = os.path.join(before_file_dir, f)  # 待移动文件的路径拼接(含文件名)
                    print(before_file)
                    copyFile(before_file, after_dir)   # 调用复制函数
    else:
        before_file_dir = before_dir + 'sub' + str(num)
        if os.path.exists(before_file_dir):
            dirs = os.listdir(before_file_dir)  # 获取指定路径下的文件
            for f in dirs:
                if os.path.splitext(f)[1] == '.nii':  # 筛选文件夹内的nii文件
                    before_file = os.path.join(before_file_dir, f)   # 待移动文件的路径拼接(含文件名)
                    print(before_file)
                    copyFile(before_file, after_dir)   # 调用复制函数
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值