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)
python指定后缀文件拷贝
最新推荐文章于 2024-02-24 15:22:33 发布