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遍历删除指定后缀文件
- python获取指定路径下所有指定后缀文件的方法
- linux下用python统计某个目录下指定后缀文件的个数
- 编写一个程序,把指定目录下的所有的带.java文件都拷贝到另一个目录中,拷贝成功后,把后缀名是.java的改成.txt。
- [Python] 删除指定目录下后缀为 xxx 的过期文件
- python脚本实现文件的批量移动、复制,以及删除指定后缀
- 将指定目录下所有的固定后缀的文件拷贝到另一个目录
- python获取指定路径下所有指定后缀文件的方法
- 技巧两种:LINUX删除指定后缀文件及PYTHON更改屏幕字色
- python 从共享文件中自动拷贝指定文件到本地【续】
- python 获取文件夹下指定后缀的文件
- mk拷贝后缀相同的文件到指定目录命令
- python获取指定路径下所有指定后缀文件的方法
- Python基础 —— 获取当前文件所在目录,遍历当前目录,获取指定后缀的文件
- python实现拷贝指定文件到指定目录
- Python读取指定目录下指定后缀文件并保存为docx
- python 从共享文件中自动拷贝指定文件到本地
- 搜索指定类型文件数量(python)
- python 脚本(获取指定文件夹、指定文件格式、的代码行数、注释行数)
- 获取指定扩展名的文件 分类: python 小练习 2013-05-14 16:06 229人阅读 评论(0) 收藏