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 26进制计算实现方法
May 28 Python
浅谈python多线程和队列管理shell程序
Aug 04 Python
在Django中进行用户注册和邮箱验证的方法
May 09 Python
Python对文件和目录进行操作的方法(file对象/os/os.path/shutil 模块)
May 08 Python
Python操作SQLite数据库的方法详解
Jun 16 Python
Python txt文件加入字典并查询的方法
Jan 15 Python
Python 移动光标位置的方法
Jan 20 Python
Python判断对象是否为文件对象(file object)的三种方法示例
Apr 26 Python
keras的siamese(孪生网络)实现案例
Jun 12 Python
Python Tkinter图形工具使用方法及实例解析
Jun 15 Python
Python中生成ndarray实例讲解
Feb 22 Python
python中如何对多变量连续赋值
Jun 03 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中this,self,parent的区别详解
2013/06/08 PHP
css图片自适应大小
2007/11/28 Javascript
FormValidate 表单验证功能代码更新并提供下载
2008/08/23 Javascript
JQuery 学习笔记01 JQuery初接触
2010/05/06 Javascript
给jQuery方法添加回调函数一款插件的应用
2013/01/21 Javascript
js数组的操作详解
2013/03/27 Javascript
jQuery bxCarousel实现图片滚动切换效果示例代码
2013/05/15 Javascript
jQuery动态添加删除select项(实现代码)
2013/09/03 Javascript
浅析jQuery1.8的几个小变化
2013/12/10 Javascript
BootstrapValidator不触发校验的实现代码
2016/09/28 Javascript
jquery uploadify如何取消已上传成功文件
2017/02/08 Javascript
微信扫码支付零云插件版实例详解
2017/04/26 Javascript
浅谈Vue的响应式原理
2019/05/30 Javascript
js canvas实现星空连线背景特效
2019/11/01 Javascript
快速了解Vue父子组件传值以及父调子方法、子调父方法
2020/07/15 Javascript
vscode中Vue别名路径提示的实现
2020/07/31 Javascript
[05:08]2014DOTA2国际邀请赛 Hao专访复仇的胜利很爽
2014/07/15 DOTA
Python实现比较两个文件夹中代码变化的方法
2015/07/10 Python
Django 添加静态文件的两种实现方法(必看篇)
2017/07/14 Python
《Python学习手册》学习总结
2018/01/17 Python
Python 获取div标签中的文字实例
2018/12/20 Python
Python一行代码实现快速排序的方法
2019/04/30 Python
Python django框架应用中实现获取访问者ip地址示例
2019/05/17 Python
Python 中pandas索引切片读取数据缺失数据处理问题
2019/10/09 Python
wxPython电子表格功能wx.grid实例教程
2019/11/19 Python
python 实现简单的FTP程序
2019/12/27 Python
python中watchdog文件监控与检测上传功能
2020/10/30 Python
Python os库常用操作代码汇总
2020/11/03 Python
html5的input的required使用中遇到的问题及解决方法
2018/04/24 HTML / CSS
梅西百货官网:Macy’s
2020/08/04 全球购物
什么是静态路由?什么是动态路由?各自的特点是什么?
2015/09/16 面试题
环境科学专业个人求职信
2013/12/15 职场文书
钱塘江大潮导游词
2015/02/03 职场文书
闪闪红星观后感
2015/06/08 职场文书
2016教师六五普法学习心得体会
2016/01/21 职场文书
MySQL索引是啥?不懂就问
2021/07/21 MySQL