Python实现文件复制删除


Posted in Python onApril 19, 2016

 用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"

    感觉是果然简单, 不过简单的原因是因为库函数丰富,语言基本特性的简单真没感觉出来。

我们再来看一个实例

本人一直用foobar2000作为音乐播放器,听歌时候把自己喜欢的歌都会特别添加到一个播放列表。

自己用iphone,同步歌曲的时候需要用到itunes,而itunes却没有我用foobar2000的精选播放列表呢~

本人只好定期把播放列表的mp3文件拷贝到一个目录,我用itunes只需同步这个目录即可
(顺便吐槽下itunes不好使,在后期我都直接用其他同步工具代替之)

播放列表是*.m3u格式的文本,用记事本打开可以看到mp3的绝对路径。

直接贴代码吧,写得比较仓促,各位将就参考下即可:

#coding=gbk  
import sys, shutil, os, string 
mp3List = "F:\\My Documents\\mp3list\\默认精选.m3u" 
destDir = "G:\\POP\\默认精选" 
 
def cpFile(srcPath): 
  fileName = os.path.basename(srcPath) 
  destPath = destDir + os.path.sep + fileName 
  if os.path.exists(srcPath) and not os.path.exists(destPath): 
    print 'cp %s %s' % (srcPath,destPath) 
    shutil.copy(srcPath,destPath) 
 
if __name__ == '__main__': 
  f = file(mp3List, 'r') 
  lists = f.readlines() 
  for i in lists: 
    cpFile(string.strip(i)) 
     
  f.close()
Python 相关文章推荐
python发布模块的步骤分享
Feb 21 Python
python基础while循环及if判断的实例讲解
Aug 25 Python
详解Python map函数及Python map()函数的用法
Nov 16 Python
python实现二叉查找树实例代码
Feb 08 Python
在pycharm中使用git版本管理以及同步github的方法
Jan 16 Python
深入了解python中元类的相关知识
Aug 29 Python
Python文件操作方法详解
Feb 09 Python
Python导入模块包原理及相关注意事项
Mar 25 Python
python相对企业语言优势在哪
Jun 12 Python
python求解汉诺塔游戏
Jul 09 Python
Python内置函数property()如何使用
Sep 01 Python
基于pycharm 项目和项目文件命名规则的介绍
Jan 15 Python
利用Python获取赶集网招聘信息前篇
Apr 18 #Python
Python Sql数据库增删改查操作简单封装
Apr 18 #Python
python使用paramiko实现远程拷贝文件的方法
Apr 18 #Python
python UNIX_TIMESTAMP时间处理方法分析
Apr 18 #Python
python动态加载包的方法小结
Apr 18 #Python
python实现按行切分文本文件的方法
Apr 18 #Python
Python获取linux主机ip的简单实现方法
Apr 18 #Python
You might like
一个简单计数器的源代码
2006/10/09 PHP
PHP+fiddler抓包采集微信文章阅读数点赞数的思路详解
2019/12/20 PHP
swoole锁的机制代码实例讲解
2021/03/04 PHP
基于jquery的表格排序
2010/09/11 Javascript
js中if语句的几种优化代码写法
2011/03/12 Javascript
Jquery事件的连接使用示例
2013/06/18 Javascript
jQuery实现鼠标滑过遮罩并高亮显示效果
2013/07/16 Javascript
3分钟写出来的Jquery版checkbox全选反选功能
2013/10/23 Javascript
Javascript之this关键字深入解析
2013/11/12 Javascript
jQuery中.live()方法的用法深入解析
2013/12/30 Javascript
Javascript中对象继承的实现小例
2014/05/12 Javascript
JS实现简易图片轮播效果的方法
2015/03/25 Javascript
jQuery带时间的日期控件代码分享
2015/08/26 Javascript
基于JavaScript实现移动端TAB触屏切换效果
2015/10/20 Javascript
AngularJS中如何使用$http对MongoLab数据表进行增删改查
2016/01/23 Javascript
js判断请求的url是否可访问,支持跨域判断的实现方法
2016/09/17 Javascript
arcgis for js 修改infowindow样式的方法
2016/11/02 Javascript
Jquery根据浏览器窗口改变调整大小的方法
2017/02/07 Javascript
jQuery实现输入框的放大和缩小功能示例
2018/07/21 jQuery
python高斯分布概率密度函数的使用详解
2019/07/10 Python
对django的User模型和四种扩展/重写方法小结
2019/08/17 Python
Python 如何优雅的将数字转化为时间格式的方法
2019/09/26 Python
使用Python完成15位18位身份证的互转功能
2019/11/06 Python
使用Python制作缩放自如的圣诞老人(圣诞树)
2019/12/25 Python
基于python实现地址和经纬度转换
2020/05/19 Python
使用 django orm 写 exists 条件过滤实例
2020/05/20 Python
阿迪达斯丹麦官网:adidas丹麦
2016/10/01 全球购物
EQVVS官网:设计师男装和女装
2018/10/24 全球购物
大专毕业生自我评价分享
2013/11/10 职场文书
总经理秘书岗位职责
2014/03/17 职场文书
2014年党员干部四风问题自我剖析材料
2014/09/29 职场文书
庆祝三八妇女节标语
2014/10/09 职场文书
趣味运动会标语口号
2015/12/26 职场文书
详解CSS玩转图片Base64编码
2021/05/25 HTML / CSS
python 单机五子棋对战游戏
2022/04/28 Python
PYTHON 使用 Pandas 删除某列指定值所在的行
2022/04/28 Python