python指定后缀文件拷贝

来源:互联网 发布:linux 内核版本 历史 编辑:程序博客网 时间:2024/06/09 12:47
import osimport shutilfrom enum import Enumclass 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)
原创粉丝点击