Python复制文件操作实例详解


Posted in Python onNovember 10, 2015

本文实例讲述了Python复制文件操作用法。分享给大家供大家参考,具体如下:

这里用python实现了一个小型的自动发版本的工具。这个“自动发版本”有点虚, 只是简单地把debug 目录下的配置文件复制到指定目录,把Release下的生成文件复制到同一指定,过滤掉不需要的文件夹(.svn),然后再往这个指定目录添加几个特定的文件。

这个是我的第一个python小程序。

下面就来看其代码的实现。

首先插入必要的库:

import os 
import os.path 
import shutil 
import time, datetime

然后就是一大堆功能函数。第一个就是把某一目录下的所有文件复制到指定目录中:

def copyFiles(sourceDir, targetDir): 
   if sourceDir.find(".svn") > 0: 
     return 
   for file in os.listdir(sourceDir): 
     sourceFile = os.path.join(sourceDir, file) 
     targetFile = os.path.join(targetDir, file) 
     if os.path.isfile(sourceFile): 
       if not os.path.exists(targetDir): 
         os.makedirs(targetDir) 
       if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))): 
           open(targetFile, "wb").write(open(sourceFile, "rb").read()) 
     if os.path.isdir(sourceFile): 
       First_Directory = False 
       copyFiles(sourceFile, targetFile)

删除一级目录下的所有文件:

def removeFileInFirstDir(targetDir): 
   for file in os.listdir(targetDir): 
     targetFile = os.path.join(targetDir, file) 
     if os.path.isfile(targetFile): 
       os.remove(targetFile)

复制一级目录下的所有文件到指定目录:

def coverFiles(sourceDir, targetDir): 
     for file in os.listdir(sourceDir): 
       sourceFile = os.path.join(sourceDir, file) 
       targetFile = os.path.join(targetDir, file) 
       #cover the files 
       if os.path.isfile(sourceFile): 
         open(targetFile, "wb").write(open(sourceFile, "rb").read())

复制指定文件到目录:

def moveFileto(sourceDir, targetDir): 
  shutil.copy(sourceDir, targetDir)

往指定目录写文本文件:

def writeVersionInfo(targetDir): 
  open(targetDir, "wb").write("Revison:")

返回当前的日期,以便在创建指定目录的时候用:

def getCurTime(): 
   nowTime = time.localtime() 
   year = str(nowTime.tm_year) 
   month = str(nowTime.tm_mon) 
   if len(month) < 2: 
     month = '0' + month 
   day = str(nowTime.tm_yday) 
   if len(day) < 2: 
     day = '0' + day 
   return (year + '-' + month + '-' + day)

然后就是主函数的实现了:

if __name__ =="__main__": 
   print "Start(S) or Quilt(Q) \n" 
   flag = True 
   while (flag): 
     answer = raw_input() 
     if 'Q' == answer: 
       flag = False 
     elif 'S'== answer : 
       formatTime = getCurTime() 
       targetFoldername = "Build " + formatTime + "-01" 
       Target_File_Path += targetFoldername
       copyFiles(Debug_File_Path,  Target_File_Path) 
       removeFileInFirstDir(Target_File_Path) 
       coverFiles(Release_File_Path, Target_File_Path) 
       moveFileto(Firebird_File_Path, Target_File_Path) 
       moveFileto(AssistantGui_File_Path, Target_File_Path) 
       writeVersionInfo(Target_File_Path+"\\ReadMe.txt") 
       print "all sucess" 
     else: 
       print "not the correct command"

希望本文所述对大家python程序设计有所帮助。

Python 相关文章推荐
Python(Tornado)模拟登录小米抢手机
Nov 12 Python
Python 处理数据的实例详解
Aug 10 Python
Pyqt实现无边框窗口拖动以及窗口大小改变
Apr 19 Python
一条命令解决mac版本python IDLE不能输入中文问题
May 15 Python
Python错误处理操作示例
Jul 18 Python
python实现自动登录后台管理系统
Oct 18 Python
Pandas 按索引合并数据集的方法
Nov 15 Python
python 申请内存空间,用于创建多维数组的实例
Dec 02 Python
Win10里python3创建虚拟环境的步骤
Jan 31 Python
通过实例解析Python return运行原理
Mar 04 Python
python 装饰器功能与用法案例详解
Mar 06 Python
python如何保存文本文件
Jun 07 Python
Python中对元组和列表按条件进行排序的方法示例
Nov 10 #Python
Python 文件管理实例详解
Nov 10 #Python
Python list操作用法总结
Nov 10 #Python
python控制台中实现进度条功能
Nov 10 #Python
使用Python发送各种形式的邮件的方法汇总
Nov 09 #Python
尝试使用Python多线程抓取代理服务器IP地址的示例
Nov 09 #Python
使用Python实现BT种子和磁力链接的相互转换
Nov 09 #Python
You might like
简单的php缓存类分享     php缓存机制
2014/01/22 PHP
PHP中变量引用与变量销毁机制分析
2014/11/15 PHP
PHP中addcslashes与stripcslashes函数用法分析
2016/01/07 PHP
Symfony2安装第三方Bundles实例详解
2016/02/04 PHP
php中如何执行linux命令详解
2018/11/06 PHP
PHP 模拟登陆功能实例详解
2019/09/10 PHP
ExtJS Store的数据访问与更新问题
2010/04/28 Javascript
当前页禁止复制粘贴截屏代码小集
2013/07/24 Javascript
javascript获取xml节点的最大值(实现代码)
2013/12/11 Javascript
JQuery中使用Ajax赋值给全局变量异常的解决方法
2014/01/10 Javascript
Blocksit插件实现瀑布流数据无限( 异步)加载
2014/06/20 Javascript
js实现的类似于asp数据字典的数据类型代码实例
2014/09/03 Javascript
JQuery仿小米手机抢购页面倒计时效果
2014/12/16 Javascript
Javascript基础教程之for循环
2015/01/18 Javascript
Node.js node-schedule定时任务隔多少分钟执行一次的方法
2015/02/10 Javascript
AngularJS快速入门
2015/04/02 Javascript
JavaScript AOP编程实例
2015/06/16 Javascript
COM组件中调用JavaScript函数详解及实例
2017/02/23 Javascript
深入理解在JS中通过四种设置事件处理程序的方法
2017/03/02 Javascript
XMLHttpRequest对象_Ajax异步请求重点(推荐)
2017/09/28 Javascript
vue component 中引入less文件报错 Module build failed
2019/04/17 Javascript
详解package.json版本号规则
2019/08/01 Javascript
Django框架中处理URLconf中特定的URL的方法
2015/07/20 Python
Python检测网络延迟的代码
2018/05/15 Python
python 实现将多条曲线画在一幅图上的方法
2019/07/07 Python
Django-migrate报错问题解决方案
2020/04/21 Python
Python xlwt模块使用代码实例
2020/06/10 Python
python读取excel进行遍历/xlrd模块操作
2020/07/12 Python
Python中生成ndarray实例讲解
2021/02/22 Python
HTML5中原生的右键菜单创建方法
2016/06/28 HTML / CSS
毕业生精彩的自我评价分享
2013/10/06 职场文书
11月升旗仪式讲话稿
2014/02/15 职场文书
3.12植树节活动总结2014
2014/03/13 职场文书
人事任命通知书
2015/04/21 职场文书
2015年招生工作总结
2015/05/04 职场文书
2016年中学植树节活动总结
2016/03/16 职场文书