python指定后缀文件拷贝

import os
import shutil
from enum import Enum


class dirStruct(Enum):
    DirNone = 1     #直接拷贝到指定的目录
    DirExt = 2      #按后缀名新建文件夹,将相同的文件拷贝到指定的目录
    DirOrigin = 3   #按照原来目录来新建目录并且拷贝文件


def copyextfile(srcpath, dstpath, ext, dirstrut):
    for root, _, files in os.walk(srcpath):
        if dirstrut is dirStruct.DirOrigin:
            newpath = root.replace(srcpath, dstpath)
            if not os.path.exists(newpath):
                os.mkdir(newpath)
            for filename in files:
                if os.path.splitext(filename)[1] in ext:
                    filepath = os.path.join(root, filename)
                    shutil.copy(filepath, newpath)

        if dirstrut is dirStruct.DirExt:
            for dirext in ext:
                dirpath = os.path.join(dstpath, dirext.lstrip('.'))
                if not os.path.exists(dirpath):
                    os.mkdir(dirpath)
            for filename in files:
                extname = os.path.splitext(filename)[1]
                if extname in ext:
                    filepath = os.path.join(root, filename)
                    newfilepath = os.path.join(dstpath, extname.lstrip('.'))
                    shutil.copy(filepath, newfilepath)

        if dirstrut is dirStruct.DirNone:
            for filename in files:
                if os.path.splitext(filename)[1] in ext:
                    filepath = os.path.join(root, filename)
                    shutil.copy(filepath, dstpath)


if __name__ == "__main__":
    srcpath = r'C:\Users\localhost\Desktop\375\RFduino'
    dstpath = r'C:\Users\localhost\Desktop\dd\HelloWorld\d\s'
    copyextfile(srcpath, dstpath, ['.c', '.h'], dirStruct.DirExt)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值