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进阶教程之词典、字典、dict
Aug 29 Python
Python实现遍历数据库并获取key的值
May 17 Python
python去掉行尾的换行符方法
Jan 04 Python
基于python log取对数详解
Jun 08 Python
详解Python中pandas的安装操作说明(傻瓜版)
Apr 08 Python
Django重置migrations文件的方法步骤
May 01 Python
如何不用安装python就能在.NET里调用Python库
Jul 12 Python
Django中间件基础用法详解
Jul 18 Python
使用python-opencv读取视频,计算视频总帧数及FPS的实现
Dec 10 Python
python GUI库图形界面开发之PyQt5信号与槽基础使用方法与实例
Mar 06 Python
Python装饰器结合递归原理解析
Jul 02 Python
selenium+超级鹰实现模拟登录12306
Jan 24 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
中国广播史趣谈 — 几个历史第一次
2021/03/01 无线电
require(),include(),require_once()和include_once()区别
2008/03/27 PHP
PHP 图片上传代码
2011/09/13 PHP
php环境配置之CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI比较?
2011/10/17 PHP
PHP远程采集图片详细教程
2014/07/01 PHP
简要剖析PHP的Yii框架的组件化机制的基本知识
2016/03/17 PHP
利用PHP如何写APP接口详解
2016/08/23 PHP
ajax调用返回php接口返回json数据的方法(必看篇)
2017/05/05 PHP
JavaScript 中的replace方法说明
2007/04/13 Javascript
jQuery实现根据类型自动显示和隐藏表单
2015/03/18 Javascript
JQuery插件jcarousellite的参数中文说明
2015/05/11 Javascript
jQuery+HTML5加入购物车代码分享
2020/10/29 Javascript
jquery简单实现带渐显效果的选项卡菜单代码
2015/09/01 Javascript
浅谈JS继承_寄生式继承 &amp; 寄生组合式继承
2016/08/16 Javascript
JS 动态判断PC和手机浏览器实现代码
2016/09/21 Javascript
vue2.0 computed 计算list循环后累加值的实例
2018/03/07 Javascript
AngularJS中重新加载当前路由页面的方法
2018/03/09 Javascript
element上传组件循环引用及简单时间倒计时的实现
2018/10/01 Javascript
javascript function(函数类型)使用与注意事项小结
2019/06/10 Javascript
js设计模式之单例模式原理与用法详解
2019/08/15 Javascript
vue 解决异步数据更新问题
2019/10/29 Javascript
vue 实现图片懒加载功能
2020/12/31 Vue.js
Python中optionParser模块的使用方法实例教程
2014/08/29 Python
python基础教程之分支、循环简单用法
2016/06/16 Python
python使用PyCharm进行远程开发和调试
2017/11/02 Python
python3多线程知识点总结
2019/09/26 Python
浅谈CSS3 动画卡顿解决方案
2019/01/02 HTML / CSS
La Senza官网:北美顶尖性感内衣品牌
2018/08/03 全球购物
韩国乐天网上商城:Lotte iMall
2021/02/03 全球购物
什么是数据抽象
2016/11/26 面试题
Linux机考试题
2015/07/17 面试题
临床医学大学生求职信
2013/09/28 职场文书
四好少年事迹材料
2014/01/12 职场文书
终止劳动合同协议书
2014/04/14 职场文书
扶贫办主任查摆“四风”问题个人对照检查材料思想汇报
2014/10/02 职场文书
慈善献爱心倡议书
2015/04/27 职场文书