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 相关文章推荐
在Apache服务器上同时运行多个Django程序的方法
Jul 22 Python
Python中easy_install 和 pip 的安装及使用
Jun 05 Python
Python中实现变量赋值传递时的引用和拷贝方法
Apr 29 Python
pycharm修改界面主题颜色的方法
Jan 17 Python
python查看文件大小和文件夹内容的方法
Jul 08 Python
python实现高斯(Gauss)迭代法的例子
Nov 20 Python
Python 定义只读属性的实现方式
Mar 05 Python
python实现FTP文件传输的方法(服务器端和客户端)
Mar 20 Python
使用Keras构造简单的CNN网络实例
Jun 29 Python
tensorflow与numpy的版本兼容性问题的解决
Jan 08 Python
python树莓派通过队列实现进程交互的程序分析
Jul 04 Python
如何Python使用re模块实现okenizer
Apr 30 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相关资料
2006/10/09 PHP
smtp邮件发送一例
2006/10/09 PHP
用PHP控制用户的浏览器--ob*函数的使用说明
2007/03/16 PHP
php 远程图片保存到本地的函数类
2008/12/08 PHP
apache+php完美解决301重定向的两种方法
2011/06/08 PHP
php的array_multisort()使用方法介绍
2012/05/16 PHP
php检测数组长度函数sizeof与count用法
2014/11/17 PHP
php去除头尾空格的2种方法
2015/03/16 PHP
jQuery使用fadein方法实现渐出效果实例
2015/03/27 Javascript
JavaScript基础教程——入门必看篇
2016/05/20 Javascript
jquery.multiselect多选下拉框实现代码
2016/11/11 Javascript
Angular多选、全选、批量选择操作实例代码
2017/03/10 Javascript
从零开始学习Node.js系列教程三:图片上传和显示方法示例
2017/04/13 Javascript
AngularJS常见过滤器用法实例总结
2017/07/06 Javascript
bootstrap table插件的分页与checkbox使用详解
2017/07/23 Javascript
vue.js中父组件调用子组件的内部方法示例
2017/10/22 Javascript
Element Carousel 走马灯的具体实现
2020/07/26 Javascript
vue 验证两次输入的密码是否一致的方法示例
2020/09/29 Javascript
[44:30]完美世界DOTA2联赛PWL S2 GXR vs Magma 第一场 11.25
2020/11/26 DOTA
python ElementTree 基本读操作示例
2009/04/09 Python
给Python IDLE加上自动补全和历史功能
2014/11/30 Python
详解Python的Django框架中manage命令的使用与扩展
2016/04/11 Python
基于Python中求和函数sum的用法详解
2018/06/28 Python
python调用自定义函数的实例操作
2019/06/26 Python
Python数据库小程序源代码
2019/09/15 Python
Python qrcode 生成一个二维码的实例详解
2020/02/12 Python
Python 私有属性和私有方法应用场景分析
2020/06/19 Python
快时尚眼镜品牌,全国连锁眼镜店:LOHO眼镜生活
2018/10/08 全球购物
Subside Sports德国:足球球衣和球迷商品
2019/06/08 全球购物
个人租房协议书
2014/04/09 职场文书
三方协议书范本
2014/04/22 职场文书
大学生交通专业求职信
2014/09/01 职场文书
2014年便民服务中心工作总结
2014/12/20 职场文书
党员证明模板
2015/06/19 职场文书
高中历史教学反思
2016/02/19 职场文书
Ruby序列化和持久化存储 Marshal和Pstore介绍
2022/04/18 Ruby