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基于TCP实现会聊天的小机器人功能示例
Apr 09 Python
python3将视频流保存为本地视频文件
Jun 20 Python
Python使用ConfigParser模块操作配置文件的方法
Jun 29 Python
python中的字符串内部换行方法
Jul 19 Python
Python实现批量修改图片格式和大小的方法【opencv库与PIL库】
Dec 03 Python
Python文件如何引入?详解引入Python文件步骤
Dec 10 Python
关于Pycharm无法debug问题的总结
Jan 19 Python
用python的turtle模块实现给女票画个小心心
Nov 23 Python
np.newaxis 实现为 numpy.ndarray(多维数组)增加一个轴
Nov 30 Python
django 取消csrf限制的实例
Mar 13 Python
jupyter notebook运行命令显示[*](解决办法)
May 18 Python
详解Python中@staticmethod和@classmethod区别及使用示例代码
Dec 14 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的Laravel框架结合MySQL与Redis数据库的使用部署
2016/03/21 PHP
PHP unset函数原理及使用方法解析
2020/08/14 PHP
javascript 操作select下拉列表框的一点小经验
2010/03/20 Javascript
ASP.NET jQuery 实例8 (动态添加内容到DropDownList)
2012/02/03 Javascript
JS HTML5 音乐天气播放器(Ajax获取天气信息)
2013/05/26 Javascript
Bootstrap的class样式小结
2016/12/01 Javascript
ES6新特性七:数组的扩充详解
2017/04/21 Javascript
nodeJS实现路由功能实例代码
2017/06/08 NodeJs
详解vue slot插槽的使用方法
2017/06/13 Javascript
package.json各个属性说明详解
2020/03/11 Javascript
vue中父子组件传值,解决钩子函数mounted只运行一次的操作
2020/07/27 Javascript
解决vue单页面应用打包后相对路径、绝对路径相关问题
2020/08/14 Javascript
vue使用vant中的checkbox实现全选功能
2020/11/17 Vue.js
[16:19]教你分分钟做大人——风暴之灵
2015/03/11 DOTA
[01:06]欢迎来到上海,TI9
2018/08/26 DOTA
下载给定网页上图片的方法
2014/02/18 Python
Python打印输出数组中全部元素
2018/03/13 Python
python可视化篇之流式数据监控的实现
2019/08/07 Python
Python 中pandas索引切片读取数据缺失数据处理问题
2019/10/09 Python
python GUI库图形界面开发之PyQt5信号与槽基本操作
2020/02/25 Python
python实现单张图像拼接与批量图片拼接
2020/03/23 Python
OpenCV+python实现实时目标检测功能
2020/06/24 Python
萌新HTML5 入门指南(二)
2020/11/09 HTML / CSS
Foot Locker德国官方网站:美国运动服和鞋类零售商
2018/11/01 全球购物
澳大利亚和新西兰最大的在线旅行社之一:Aunt Betty
2019/08/07 全球购物
美国购买体育、音乐会和剧院门票网站:SelectATicket
2019/09/08 全球购物
值类型与引用类型有什么不同?请举例说明?并分别列举几种相应的数据类型
2015/10/24 面试题
能否解释一下XSS cookie盗窃是什么意思
2012/06/02 面试题
高三历史教学反思
2014/01/09 职场文书
函授大学生自我鉴定
2014/02/05 职场文书
公证委托书模板
2014/04/03 职场文书
合伙协议书范本
2014/04/21 职场文书
创先争优承诺书
2015/01/20 职场文书
2015教师个人工作总结范文
2015/03/31 职场文书
廉洁自律承诺书范文
2015/04/28 职场文书
详解Redis实现限流的三种方式
2021/04/27 Redis