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实现递归版汉诺塔示例(汉诺塔递归算法)
Apr 08 Python
python编程使用selenium模拟登陆淘宝实例代码
Jan 25 Python
Python模拟随机游走图形效果示例
Feb 06 Python
Python实现的计算器功能示例
Apr 26 Python
python3实现windows下同名进程监控
Jun 21 Python
使用pandas批量处理矢量化字符串的实例讲解
Jul 10 Python
让你Python到很爽的加速递归函数的装饰器
May 26 Python
python实现查找所有程序的安装信息
Feb 18 Python
如何创建一个Flask项目并进行简单配置
Nov 18 Python
Python基础之进程详解
May 21 Python
Python还能这么玩之用Python做个小游戏的外挂
Jun 04 Python
Python 恐龙跑跑小游戏实现流程
Feb 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
phpMyAdmin 链接表的附加功能尚未激活的问题
2010/08/01 PHP
php+MySQL判断update语句是否执行成功的方法
2014/08/28 PHP
php使用Jpgraph绘制柱形图的方法
2015/06/10 PHP
PHP登录验证码的实现与使用方法
2016/07/07 PHP
CI框架AR数据库操作常用函数总结
2016/11/21 PHP
CakePHP框架Model函数定义方法示例
2017/08/04 PHP
在 Laravel 项目中使用 webpack-encore的方法
2019/07/21 PHP
jQuery 选择器、DOM操作、事件、动画
2010/11/25 Javascript
js获取触发事件元素在整个网页中的绝对坐标(示例代码)
2013/12/13 Javascript
javascript数组去重方法汇总
2015/04/23 Javascript
Laydate时间组件在火狐浏览器下有多时间输入框时只能给第一个输入框赋值的解决方法
2016/08/18 Javascript
Javascript+CSS3实现进度条效果
2016/10/28 Javascript
jQuery实现可移动选项的左右下拉列表示例
2016/12/26 Javascript
angular ng-click防止重复提交实例
2017/06/16 Javascript
使用JavaScript实现node.js中的path.join方法
2018/08/12 Javascript
解决Angular4项目部署到服务器上刷新404的问题
2018/08/31 Javascript
node使用Mongoose类库实现简单的增删改查
2018/11/08 Javascript
vue 实现通过vuex 存储值 在不同界面使用
2019/11/11 Javascript
python脚本实现分析dns日志并对受访域名排行
2014/09/18 Python
python根据出生日期获得年龄的方法
2015/03/31 Python
常见python正则用法的简单实例
2016/06/21 Python
python中字符串类型json操作的注意事项
2017/05/02 Python
Python import与from import使用及区别介绍
2018/09/06 Python
python如何查看微信消息撤回
2018/11/27 Python
Django框架封装外部函数示例
2019/05/28 Python
Python从函数参数类型引出元组实例分析
2019/05/28 Python
keras slice layer 层实现方式
2020/06/11 Python
python两种注释用法的示例
2020/10/09 Python
求职信需要的五点内容
2014/02/01 职场文书
《陶罐和铁罐》教学反思
2014/02/19 职场文书
考核评语大全
2014/04/29 职场文书
党支部创先争优承诺书
2014/08/30 职场文书
2015年社区卫生工作总结
2015/04/21 职场文书
国王的演讲观后感
2015/06/03 职场文书
公司仓库管理制度
2015/08/04 职场文书
python 多态 协议 鸭子类型详解
2021/11/27 Python