您的位置:首页 > 编程语言 > Python开发

python指定后缀文件拷贝

2017-07-20 15:35 141 查看
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 os