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通过PIL获取图片主要颜色并和颜色库进行对比的方法
Mar 19 Python
Python实现的多线程http压力测试代码
Feb 08 Python
matplotlib作图添加表格实例代码
Jan 23 Python
11个Python3字典内置方法大全与示例汇总
May 13 Python
Python 迭代,for...in遍历,迭代原理与应用示例
Oct 12 Python
python 实现return返回多个值
Nov 19 Python
基于python检查矩阵计算结果
May 21 Python
tensorflow实现残差网络方式(mnist数据集)
May 26 Python
python 监控logcat关键字功能
Sep 04 Python
Django windows使用Apache实现部署流程解析
Oct 12 Python
Python实现随机爬山算法
Jan 29 Python
python之openpyxl模块的安装和基本用法(excel管理)
Feb 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猴子选大王问题解决方法
2015/05/12 PHP
thinkphp中字符截取函数msubstr()用法分析
2016/01/09 PHP
简单PHP会话(session)说明介绍
2016/08/21 PHP
php实现批量上传数据到数据库(.csv格式)的案例
2017/06/18 PHP
PHP实现的折半查找算法示例
2017/12/19 PHP
jquery 1.3.2 IE8中的一点点的小问题解决方法
2009/07/10 Javascript
jQuery UI AutoComplete 使用说明
2011/06/20 Javascript
jQuery中读取json文件示例代码
2013/05/10 Javascript
js单例模式的两种方案
2013/10/22 Javascript
js同比例缩放图片的小例子
2013/10/30 Javascript
jquery判断密码强度的验证代码
2020/04/22 Javascript
javascript中sort排序实例详解
2016/07/24 Javascript
node.js请求HTTPS报错:UNABLE_TO_VERIFY_LEAF_SIGNATURE\的解决方法
2016/12/18 Javascript
vue-cli 默认路由再子路由选中下的选中状态问题及解决代码
2018/09/06 Javascript
利用PHP实现递归删除链表元素的方法示例
2020/10/23 Javascript
vue项目如何监听localStorage或sessionStorage的变化
2021/01/04 Vue.js
[05:56]第十六期——新进3大C之小兔基
2014/06/24 DOTA
[02:23]2014DOTA2国际邀请赛中国战队回顾
2014/08/01 DOTA
Python中使用item()方法遍历字典的例子
2014/08/26 Python
Python图片裁剪实例代码(如头像裁剪)
2017/06/21 Python
python 获取毫秒数,计算调用时长的方法
2019/02/20 Python
Python-接口开发入门解析
2019/08/01 Python
使用Python完成15位18位身份证的互转功能
2019/11/06 Python
详解Tensorflow不同版本要求与CUDA及CUDNN版本对应关系
2020/08/04 Python
详解Python中@staticmethod和@classmethod区别及使用示例代码
2020/12/14 Python
CSS3感应鼠标的背景闪烁和图片缩放动画效果
2014/05/14 HTML / CSS
CSS3 Pie工具推荐--让IE6-8支持一些优秀的CSS3特性
2014/09/02 HTML / CSS
深入了解canvas在移动端绘制模糊的问题解决
2019/04/30 HTML / CSS
欧洲著名的二手奢侈品网站:Vestiaire Collective
2020/03/07 全球购物
施惠特软件测试面试题以及笔试题
2015/05/13 面试题
学校周年庆活动方案
2014/08/22 职场文书
工作态度不端正检讨书
2014/10/04 职场文书
党的群众路线教育实践活动个人整改落实情况汇报
2014/10/28 职场文书
武侯祠导游词
2015/02/04 职场文书
舞出我人生观后感
2015/06/16 职场文书
交通事故责任认定书
2015/08/06 职场文书