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绘制人人网好友关系图示例
Apr 01 Python
Python中的两个内置模块介绍
Apr 05 Python
Python本地与全局命名空间用法实例
Jun 16 Python
Python中线程编程之threading模块的使用详解
Jun 23 Python
Python获取文件所在目录和文件名的方法
Jan 12 Python
pyQT5 实现窗体之间传值的示例
Jun 20 Python
200行python代码实现2048游戏
Jul 17 Python
在Pytorch中计算自己模型的FLOPs方式
Dec 30 Python
Win10里python3创建虚拟环境的步骤
Jan 31 Python
PyCharm无法引用自身项目解决方式
Feb 12 Python
QT5 Designer 打不开的问题及解决方法
Aug 20 Python
python调用ffmpeg命令行工具便捷操作视频示例实现过程
Nov 01 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
Windows下PHP5和Apache的安装与配置
2006/09/05 PHP
使用NetBeans + Xdebug调试PHP程序的方法
2011/04/12 PHP
关于php支持分块与断点续传文件下载功能代码
2014/05/09 PHP
PHP列出MySQL中所有数据库的方法
2015/03/12 PHP
php实现模拟登陆方正教务系统抓取课表
2015/05/19 PHP
php+javascript实现的动态显示服务器运行程序进度条功能示例
2017/08/07 PHP
用Javascript实现UTF8编码转换成gb2312编码
2006/12/22 Javascript
在JavaScript中监听IME键盘输入事件
2011/05/29 Javascript
javascript实现焦点滚动图效果 具体方法
2013/06/24 Javascript
jquery live()调用不存在的解决方法
2014/02/26 Javascript
JavaScript作用域链示例分享
2014/05/27 Javascript
js简单工厂模式用法实例
2015/06/30 Javascript
JS实现页面数据无限加载
2016/09/13 Javascript
使用jquery如何获取时间
2016/10/13 Javascript
ES6中Generator与异步操作实例分析
2017/03/31 Javascript
详解在Angular项目中添加插件ng-bootstrap
2017/07/04 Javascript
详解vue填坑之解决部分浏览器不支持pushState方法
2018/07/12 Javascript
layer插件select选中默认值的方法
2018/08/14 Javascript
谈谈JavaScript中super(props)的重要性
2019/02/12 Javascript
推荐一个基于Node.js的表单验证库
2019/02/15 Javascript
如何在微信小程序中实现Mixins方案
2019/06/20 Javascript
JavaScript进制转换实现方法解析
2020/01/18 Javascript
vue二选一tab栏切换新做法实现
2021/01/19 Vue.js
[01:20]DOTA2 2017国际邀请赛冠军之路无止竞
2017/06/19 DOTA
python 文件和路径操作函数小结
2009/11/23 Python
python快速排序代码实例
2013/11/21 Python
跟老齐学Python之传说中的函数编写条规
2014/10/11 Python
Python操作列表之List.insert()方法的使用
2015/05/20 Python
python按照多个条件排序的方法
2019/02/08 Python
简单了解python的内存管理机制
2019/07/08 Python
python爬虫神器Pyppeteer入门及使用
2019/07/13 Python
一款CSS3实现多功能下拉菜单(带分享按)的教程
2014/11/05 HTML / CSS
逃课检讨书范文
2015/05/06 职场文书
导游词之山西-五老峰
2019/10/07 职场文书
Python爬虫中urllib3与urllib的区别是什么
2021/07/21 Python
JavaScript架构搭建前端监控如何采集异常数据
2022/06/25 Javascript