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 list中append()与extend()用法分享
Mar 24 Python
python开启多个子进程并行运行的方法
Apr 18 Python
编写Python的web框架中的Model的教程
Apr 29 Python
python if not in 多条件判断代码
Sep 21 Python
Python基于回溯法子集树模板解决马踏棋盘问题示例
Sep 11 Python
Python如何实现MySQL实例初始化详解
Nov 06 Python
使用pandas对矢量化数据进行替换处理的方法
Apr 11 Python
Python中数组,列表:冒号的灵活用法介绍(np数组,列表倒序)
Apr 18 Python
Python读取YUV文件,并显示的方法
Dec 04 Python
使用python 打开文件并做匹配处理的实例
Jan 02 Python
python读写csv文件并增加行列的实例代码
Aug 01 Python
Django对接elasticsearch实现全文检索的示例代码
Aug 02 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生成excel列名超过26列大于Z时的解决方法
2014/12/29 PHP
浅谈使用PHP开发微信支付的流程
2015/10/04 PHP
php curl模拟post请求和提交多维数组的示例代码
2015/11/19 PHP
PHP使Laravel为JSON REST API返回自定义错误的问题
2018/10/16 PHP
Laravel框架实现的rbac权限管理操作示例
2019/01/16 PHP
PHP 对象继承原理与简单用法示例
2020/04/21 PHP
javascript 页面划词搜索JS
2009/09/28 Javascript
JavaScript操作XML实例代码(获取新闻标题并分页,并分页)
2010/05/25 Javascript
添加JavaScript重载函数的辅助方法2
2010/07/04 Javascript
javascript获取鼠标位置部分的实例代码(兼容IE,FF)
2013/08/05 Javascript
javascript实现加载xml文件的方法
2015/11/24 Javascript
js实现千分符和保留几位小数的简单实例
2016/08/01 Javascript
详解js的事件处理函数和动态创建html标记方法
2016/12/16 Javascript
Angular1.x复杂指令实例详解
2017/03/01 Javascript
JS中type=&quot;button&quot;和type=&quot;submit&quot;的区别
2017/07/04 Javascript
js获取文件里面的所有文件名(实例)
2017/10/17 Javascript
NodeJs实现定时任务的示例代码
2017/12/05 NodeJs
JavaScript模块详解
2017/12/18 Javascript
JavaScript实现的简单Tab点击切换功能示例
2018/07/06 Javascript
nodejs中实现用户注册路由功能
2019/05/20 NodeJs
layui实现form表单同时提交数据和文件的代码
2019/10/25 Javascript
在Vuex中Mutations修改状态操作
2020/07/24 Javascript
[05:06]DOTA2-DPC中国联赛 正赛 VG vs Magma选手采访
2021/03/11 DOTA
python解析模块(ConfigParser)使用方法
2013/12/10 Python
python友情链接检查方法
2015/07/08 Python
python如何爬取网站数据并进行数据可视化
2019/07/08 Python
CSS 3.0 结合video视频实现的创意开幕效果
2020/06/01 HTML / CSS
浅析HTML5的WebSocket与服务器推送事件
2016/02/19 HTML / CSS
加拿大快时尚零售商:Ardene
2018/02/14 全球购物
英国家用电器购物网站:Hughes
2018/02/23 全球购物
美国CVS药店官网:CVS Pharmacy
2018/07/26 全球购物
Ibood荷兰:互联网每日最佳在线优惠
2019/02/28 全球购物
安全生产责任书
2014/03/12 职场文书
法定代表人授权委托书格式
2014/10/14 职场文书
python 中的jieba分词库
2021/11/23 Python
海弦WR-800F
2022/04/05 无线电