python拷贝文件
来源:互联网 发布:透明罗盘软件 编辑:程序博客网 时间:2024/06/09 12:48
在做cocos2dx-js的时候每次打测试包都要拷贝js到打包环境中,但是又有很多文件不想拷贝进去,所以就用python写了一个脚本来进行拷贝粘贴,顺便学习了一下python。代码非常简单,第一个功能是直接拷贝整个文件夹,第二个功能是遍历判定后缀名拷贝,但是还是会创建文件夹路径(暂时没有优化掉,我的一个思路是先判定当前路径树下是否有需要拷贝的文件,但是感觉复杂度瞬间就高了,暂时没有好的解决方案,先堆积),直接贴代码:
#coding=utf-8import shutilimport osclass CopyFileUtil: @classmethod def CopyFileFromTo(cls, strFrom, strTo): if os.path.isfile(strFrom): shutil.copyfile(strFrom, strTo) else: for fileName in os.listdir(strFrom): sourceFileName = os.path.join(strFrom, fileName) targetFileName = os.path.join(strTo, fileName) #如果源文件一个文件 #1.判断目标路径是否存在 不存在那么创建 #2.判断目标文件是否存在 if os.path.isfile(sourceFileName): if not os.path.exists(strTo): os.makedirs(strTo) if not os.path.exists(targetFileName): shutil.copyfile(sourceFileName, targetFileName) if (os.path.exists(targetFileName) and (os.path.getsize(targetFileName) != os.path.getsize(sourceFileName))): shutil.copyfile(sourceFileName, targetFileName) if os.path.isdir(sourceFileName): if os.path.exists(targetFileName): shutil.rmtree(targetFileName) shutil.copytree(sourceFileName, targetFileName) @classmethod def CopyTreeFromToWithFileType(cls, fileTypeArr, strFrom, strTo): if os.path.isfile(strTo): print("error") return if not os.path.exists(strTo): os.makedirs(strTo) CopyFileUtil.CopyTreeFromToWithFileTypeRecursion(fileTypeArr, strFrom, strTo) @classmethod def CopyTreeFromToWithFileTypeRecursion(cls, fileTypeArr, strFrom, strTo): if os.path.isfile(strFrom): strTempFileType = strFrom.split(".")[-1] if strTempFileType in fileTypeArr: shutil.copyfile(strFrom, strTo) return else: if not os.listdir(strFrom): return for fileName in os.listdir(strFrom): sourceFileName = os.path.join(strFrom, fileName) targetFileName = os.path.join(strTo, fileName) if os.path.isfile(sourceFileName): strTempFileType = sourceFileName.split(".")[-1] if strTempFileType in fileTypeArr: shutil.copyfile(sourceFileName, targetFileName) if os.path.isdir(sourceFileName): if not os.path.exists(targetFileName): os.makedirs(targetFileName) CopyFileUtil.CopyTreeFromToWithFileTypeRecursion(fileTypeArr, sourceFileName, targetFileName)CopyFileUtil.CopyTreeFromToWithFileType(["txt", "docx"], "H:/123", "H:/target")
0 0
- python文件拷贝代码
- Python文件拷贝-shutil
- Python 拷贝文件
- python文件拷贝
- python拷贝文件
- python拷贝文件
- 【Python】删除和拷贝文件
- python指定后缀文件拷贝
- Python文件操作--文件拷贝&文件合并
- 【python】通过paramiko远程拷贝文件
- python 实现文件的递归拷贝-转
- python文件拷贝工具shutil介绍
- Python行读取文件进行拷贝
- python 文件拷贝copy操作 shutill
- 第17个python程序:拷贝文件
- 用python代码实现文件的拷贝
- python拷贝文件到U盘
- Python实现文件拷贝和解开权限
- SELinux策略语言--类型强制(编写TE规则)
- ZigZag Conversion
- Java调用Shell脚本
- 性能分析工具之-- Eclipse Memory Analyzer tool(MAT)(一)
- IPC之Messenger
- python拷贝文件
- WebSocket 是什么原理?为什么可以实现持久连接?
- Pixhawk-姿态解算源码注释
- 一致性哈希算法(consistent hashing)
- js && jquery速查手册
- GradientDrawable 小知识
- 设计模式之解释器模式
- 获取某一个目录中的所有文件列表
- Facebook未来10年要做的事,只有疯狂可以形容