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在Windows和在Linux下调用动态链接库的教程
Aug 18 Python
在arcgis使用python脚本进行字段计算时是如何解决中文问题的
Oct 18 Python
Python使用functools模块中的partial函数生成偏函数
Jul 02 Python
视觉直观感受若干常用排序算法
Apr 13 Python
mac安装pytorch及系统的numpy更新方法
Jul 26 Python
Python实现正则表达式匹配任意的邮箱方法
Dec 20 Python
python实现批量文件重命名
Oct 31 Python
python绘制规则网络图形实例
Dec 09 Python
tensorflow中tf.reduce_mean函数的使用
Apr 19 Python
Python jieba结巴分词原理及用法解析
Nov 05 Python
Python调用系统命令os.system()和os.popen()的实现
Dec 31 Python
Matlab求解数组中的最大值及它所在的具体位置
Apr 16 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
全国FM电台频率大全 - 5 内蒙古自治区
2020/03/11 无线电
用PHP实现多服务器共享SESSION数据的方法
2007/03/16 PHP
php中将时间差转换为字符串提示的实现代码
2011/08/08 PHP
ThinkPHP设置禁止百度等搜索引擎转码(简单实用)
2016/02/15 PHP
ucenter中词语过滤原理分析
2016/07/13 PHP
php关联数组与索引数组及其显示方法
2018/03/12 PHP
PHP下载文件函数与用法示例
2019/09/27 PHP
关于onScroll事件在IE6下每次滚动触发三次bug说明
2011/09/21 Javascript
JS获取各种宽度、高度的简单介绍
2014/12/19 Javascript
NodeJS学习笔记之MongoDB模块
2015/01/13 NodeJs
jQuery内部原理和实现方式浅析
2015/02/03 Javascript
JavaScript创建一个object对象并操作对象属性的用法
2015/03/23 Javascript
javascript跨域方法、原理以及出现问题解决方法(详解)
2015/08/06 Javascript
jQuery实现点击水纹波动动画
2016/04/10 Javascript
第四章之BootStrap表单与图片
2016/04/25 Javascript
jQuery实现的简单排序功能示例【冒泡排序】
2017/01/13 Javascript
使用 Node.js 实现图片的动态裁切及算法实例代码详解
2018/09/29 Javascript
JavaScript实现的九种排序算法
2019/03/04 Javascript
实例详解vue中的$root和$parent
2019/04/29 Javascript
Node 模块原理与用法详解
2020/05/13 Javascript
理解Proxy及使用Proxy实现vue数据双向绑定操作
2020/07/18 Javascript
[56:29]Secret vs Optic 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
利用Python中的mock库对Python代码进行模拟测试
2015/04/16 Python
详谈pandas中agg函数和apply函数的区别
2018/04/20 Python
Django unittest 设置跳过某些case的方法
2018/12/26 Python
python3 线性回归验证方法
2019/07/09 Python
pytorch索引查找 index_select的例子
2019/08/18 Python
如何利用python给图片添加半透明水印
2019/09/06 Python
Python注释、分支结构、循环结构、伪“选择结构”用法实例分析
2020/01/09 Python
Spartoo葡萄牙鞋类网站:线上销售鞋履与时尚配饰
2017/01/11 全球购物
Viking Direct爱尔兰:办公用品和家具
2019/11/21 全球购物
初三化学教学反思
2014/01/23 职场文书
女子职高个人自荐书
2014/02/01 职场文书
共产党员岗位承诺书
2014/05/29 职场文书
职代会闭幕词
2015/01/28 职场文书
mysql 索引合并的使用
2021/08/30 MySQL