- #_*_encoding:utf-8_*_
- import sys
- import os
- import shutil
- import platform
- class FileUtils:
- @staticmethod
- def fileFilterExt(args, dirn, fln):
- for fls in fln:
- if fls.lower().endswith(args[1].lower()) and os.path.isfile(os.path.join(dirn, fls)):
- args[0].append(os.path.join(dirn,fls))
- @staticmethod
- def dirFilterExt(args, dirn, fln):
- for fls in fln:
- if fls.lower().endswith(args[1].lower()) and os.path.isdir(os.path.join(dirn, fls)):
- args[0].append(os.path.join(dirn,fls))
- # 根据文件扩展名获取文件
- @staticmethod
- def getFiles(root, ext):
- fileList = list()
- os.path.walk(root, FileUtils.fileFilterExt, (fileList, ext))
- return fileList
- # 获取文件夹
- @staticmethod
- def getDirs(root, ext):
- dirList = list()
- os.path.walk(root, FileUtils.dirFilterExt, (dirList, ext))
- return dirList
- # 复制文件到指定目录
- @staticmethod
- def copyFileExt(src, dst):
- if not os.path.exists(src):
- print(str.format("%s is not exists", src))
- return
- dirList = FileUtils.getDirs(src, "")
- for d in dirList:
- subDir = d[len(src) + 1:]
- if not os.path.exists(os.path.join(dst, subDir)):
- os.mkdir(os.path.join(dst, subDir))
- fileList = FileUtils.getFiles(src, "")
- for f in fileList:
- subName = f[len(src) + 1:]
- shutil.copy(f, os.path.join(dst, subName))
- #该片段来自于http://www.codesnippet.cn/detail/0207201512990.html
来源: http://www.codesnippet.cn/detail/0207201512990.html