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文件和目录操作方法大全(含实例)
Mar 12 Python
Python上传package到Pypi(代码简单)
Feb 06 Python
python 写的一个爬虫程序源码
Feb 28 Python
浅谈Python 中整型对象的存储问题
May 16 Python
python负载均衡的简单实现方法
Feb 04 Python
推荐10款最受Python开发者欢迎的Python IDE
Sep 16 Python
django中ORM模型常用的字段的使用方法
Mar 05 Python
Python如何访问字符串中的值
Feb 09 Python
Django crontab定时任务模块操作方法解析
Sep 10 Python
Pycharm配置autopep8实现流程解析
Nov 28 Python
用python实现监控视频人数统计
May 21 Python
利用Python+OpenCV三步去除水印
May 28 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
在线短消息收发的程序,不用数据库
2006/10/09 PHP
php+mysql查询优化简单实例
2015/01/13 PHP
Zend Framework教程之分发器Zend_Controller_Dispatcher用法详解
2016/03/07 PHP
PHP封装XML和JSON格式数据接口操作示例
2019/03/06 PHP
javascript IFrame 强制刷新代码
2009/07/23 Javascript
移动端使用localResizeIMG4压缩图片
2017/04/22 Javascript
bootstrap daterangepicker汉化以及扩展功能
2017/06/15 Javascript
Angular 5.0 来了! 有这些大变化
2017/11/15 Javascript
vue 项目常用加载器及配置详解
2018/01/22 Javascript
Node+OCR实现图像文字识别功能
2018/11/26 Javascript
js new Date()实例测试
2019/10/31 Javascript
如何在JavaScript中使用localStorage详情
2021/02/04 Javascript
[02:32]DOTA2亚洲邀请赛 VG战队巡礼
2015/02/03 DOTA
Python生成随机MAC地址
2015/03/10 Python
用Python操作字符串之rindex()方法的使用
2015/05/19 Python
tensorflow TFRecords文件的生成和读取的方法
2018/02/06 Python
python绘制中国大陆人口热力图
2018/11/07 Python
详解pyppeteer(python版puppeteer)基本使用
2019/06/12 Python
python爬虫之自制英汉字典
2019/06/24 Python
PyTorch实现AlexNet示例
2020/01/14 Python
python GUI库图形界面开发之PyQt5中QMainWindow, QWidget以及QDialog的区别和选择
2020/02/26 Python
python函数map()和partial()的知识点总结
2020/05/26 Python
python pandas dataframe 去重函数的具体使用
2020/07/20 Python
Django如何实现防止XSS攻击
2020/10/13 Python
一款基于css3的列表toggle特效实例教程
2015/01/04 HTML / CSS
css3实现冲击波效果的示例代码
2018/01/11 HTML / CSS
Fairyseason:为个人和批发商提供女装和配件
2017/03/01 全球购物
理肤泉英国官网:La Roche-Posay英国
2019/01/14 全球购物
静态成员和非静态成员的区别
2012/05/12 面试题
儿科护士自我鉴定
2013/10/14 职场文书
《小池塘》教学反思
2014/02/28 职场文书
蟋蟀的住宅教学反思
2014/04/26 职场文书
孩子教育的心得体会
2014/09/01 职场文书
婚礼迎宾词大全
2015/08/10 职场文书
护士医德医风心得体会
2016/01/25 职场文书
DSP接收机前端设想
2022/04/05 无线电