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 14 Python
Python中的面向对象编程详解(下)
Apr 13 Python
浅谈Python中函数的参数传递
Jun 21 Python
python将ansible配置转为json格式实例代码
May 15 Python
高质量Python代码编写的5个优化技巧
Nov 16 Python
Python使用三种方法实现PCA算法
Dec 12 Python
tensorflow1.0学习之模型的保存与恢复(Saver)
Apr 23 Python
python3 中的字符串(单引号、双引号、三引号)以及字符串与数字的运算
Jul 18 Python
详细介绍Python进度条tqdm的使用
Jul 31 Python
Pytorch中accuracy和loss的计算知识点总结
Sep 10 Python
Django Form and ModelForm的区别与使用
Dec 06 Python
Python爬虫之Selenium库的使用方法
Jan 03 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
PHP反射API示例分享
2016/10/08 PHP
基于js disabled=&quot;false&quot;不起作用的解决办法
2013/06/26 Javascript
js获取控件位置以及不同浏览器中的差别介绍
2013/08/08 Javascript
原生JS操作网页给p元素添加onclick事件及表格隔行变色
2013/12/01 Javascript
JS将制定内容复制到剪切板示例代码
2014/02/11 Javascript
js设置控件的隐藏与显示的两种方法
2014/08/21 Javascript
node.js中的fs.unlinkSync方法使用说明
2014/12/15 Javascript
jquery通过ajax加载一段文本内容的方法
2015/01/15 Javascript
浅谈jquery回调函数callback的使用
2015/01/30 Javascript
Vuejs第八篇之Vuejs组件的定义实例解析
2016/09/05 Javascript
jQuery为某个div加入行样式
2017/06/09 jQuery
NodeJS父进程与子进程资源共享原理与实现方法
2018/03/16 NodeJs
JavaScript日期工具类DateUtils定义与用法示例
2018/09/03 Javascript
express express-session的使用小结
2018/12/12 Javascript
详解JavaScript中的坐标和距离
2019/05/27 Javascript
微信小程序实现树莓派(raspberry pi)小车控制
2020/02/12 Javascript
[03:38]2014DOTA2西雅图国际邀请赛 VG战队巡礼
2014/07/07 DOTA
[43:26]完美世界DOTA2联赛PWL S2 Forest vs Rebirth 第二场 11.20
2020/11/23 DOTA
python将unicode转为str的方法
2017/06/21 Python
PyQt5每天必学之QSplitter实现窗口分隔
2018/04/19 Python
Selenium基于PIL实现拼接滚动截图
2020/04/10 Python
pyspark给dataframe增加新的一列的实现示例
2020/04/24 Python
python如何构建mock接口服务
2021/01/28 Python
CSS3 选择器 属性选择器介绍
2012/01/21 HTML / CSS
美国渔具店:FishUSA
2019/08/07 全球购物
英国奢华护肤、美容和Spa品牌:Temple Spa
2019/11/02 全球购物
嘻哈珠宝品牌:KRKC&CO
2020/10/19 全球购物
laravel使用redis队列实例讲解
2021/03/23 PHP
机电一体化专业应届本科生求职信
2013/09/27 职场文书
应届生会计求职信
2013/11/11 职场文书
大学生村官个人对照检查材料(群众路线)
2014/09/26 职场文书
大学生暑期社会实践证明范本
2014/10/24 职场文书
党的群众路线学习笔记
2014/11/06 职场文书
2014年政协工作总结
2014/12/09 职场文书
面试复试通知单
2015/04/24 职场文书
发言稿之优秀教师篇
2019/09/26 职场文书