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实现约瑟夫环问题的方法
May 03 Python
python读取excel表格生成erlang数据
Aug 26 Python
win10下python3.5.2和tensorflow安装环境搭建教程
Sep 19 Python
Django中ORM外键和表的关系详解
May 20 Python
Pandas之排序函数sort_values()的实现
Jul 09 Python
python函数装饰器之带参数的函数和带参数的装饰器用法示例
Nov 06 Python
tensorflow将图片保存为tfrecord和tfrecord的读取方式
Feb 17 Python
python matplotlib模块基本图形绘制方法小结【直线,曲线,直方图,饼图等】
Apr 26 Python
Python 字符串池化的前提
Jul 03 Python
Python中Yield的基本用法
Oct 18 Python
Python数据分析之pandas读取数据
Jun 02 Python
python turtle绘制多边形和跳跃和改变速度特效
Mar 16 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模板技术[转]
2007/01/04 PHP
PHP使用Session遇到的一个Permission denied Notice解决办法
2014/07/30 PHP
php实现求相对时间函数
2015/06/15 PHP
PHPExcel笔记, mpdf导出
2016/05/03 PHP
如何用PHP做到页面注册审核
2017/03/02 PHP
新页面打开实际尺寸的图片
2006/08/25 Javascript
用Javascript 和 CSS 实现脚注(Footnote)效果
2009/09/09 Javascript
Javascript Ajax异步读取RSS文档具体实现
2013/12/12 Javascript
jQuery Trim去除字符串首尾空字符的实现方法说明
2014/02/11 Javascript
jQuery回调函数的定义及用法实例
2014/12/23 Javascript
jQuery源码解读之removeAttr()方法分析
2015/02/20 Javascript
jQuery实现鼠标经过图片变亮其他变暗效果
2015/05/08 Javascript
JavaScript入门系列之知识点总结
2016/03/24 Javascript
jQuery Masonry瀑布流布局神器使用详解
2017/05/25 jQuery
NodeJS自定义模块写法(详解)
2017/06/27 NodeJs
ES6/JavaScript使用技巧分享
2017/12/14 Javascript
JS中获取 DOM 元素的绝对位置实例详解
2018/04/23 Javascript
vue中的计算属性实例详解
2018/09/19 Javascript
详解VUE前端按钮权限控制
2019/04/26 Javascript
uni-app从安装到卸载的入门教程
2020/05/15 Javascript
JS 5种遍历对象的方式
2020/06/16 Javascript
Python编程修改MP3文件名称的方法
2017/04/19 Python
使用python在本地电脑上快速处理数据
2017/06/22 Python
Python字符串和字典相关操作的实例详解
2017/09/23 Python
python使用itchat实现手机控制电脑
2018/02/22 Python
flask/django 动态查询表结构相同表名不同数据的Model实现方法
2019/08/29 Python
Python如何利用Har文件进行遍历指定字典替换提交的数据详解
2020/11/05 Python
详解如何通过H5(浏览器/WebView/其他)唤起本地app
2017/12/11 HTML / CSS
艺术用品:Arteza
2018/11/25 全球购物
无故旷工检讨书
2014/01/26 职场文书
奥利奥广告词
2014/03/20 职场文书
大学生活自我评价
2014/04/09 职场文书
纠纷协议书
2014/04/16 职场文书
文明班级建设方案
2014/05/15 职场文书
2015暑期社会实践个人总结
2015/07/13 职场文书
Java数据结构之堆(优先队列)
2022/05/20 Java/Android