python拷贝文件
2016-08-02 18:40
295 查看
import os, os.path, shutil, time, datetime #copy file from one dir to another def copyFiles(sourceDir, targetDir): 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): copyFiles(sourceFile, targetFile) #remove file from dir 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) if os.path.isfile(sourceFile): open(targetFile, "wb").write(open(sourceFile, "rb").read()) # def moveFileto(sourceDir, targetDir): if not os.path.exists(targetDir): os.makedirs(targetDir) shutil.copy(sourceDir, targetDir) #write data into file def writeVersionInfo(targetFile): open(targetFile, "wb").write("Revison:") #get current time 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 #Main Target_File_Path = "/Users/jianan/Desktop/" Debug_File_Path = "/Users/jianan/Desktop/Test" if __name__ == "__main__": print "Start(S) or Quit(Q);n" flag = True while flag: answer = raw_input() if 'Q' == answer: flag = False elif 'S' == answer: formatTime = getCurTime() targetFoldername = "Build" + formatTime Target_File_Path += targetFoldername coverFiles(Debug_File_Path, Target_File_Path) # copyFiles(Debug_File_Path, Target_File_Path) # removeFileInFirstDir(Target_File_Path) # moveFileto(Debug_File_Path+"/python.txt", Target_File_Path) # writeVersionInfo(Target_File_Path + "\\ReadMe.txt") print "Done..." else: print "not the correct command"
相关文章推荐
- python 实现txt文件转txt文件 windows下
- python----抽象类
- python学习-str list
- python装饰器
- python正则表达式学习笔记(上)
- Python杨辉三角算法
- Windows7安装numpy、scipy和matplotlib时遇到的问题及解决办法
- Python函数的说明与运用
- 【Ceph】基于Tornado的Ceph文件上传与下载及在线预览
- 《python核心编》程课后习题——第三章
- Python--字典
- python中*args **kwargs的使用
- 去除行号(Python3)
- Python Day2
- python开发简单爬虫:实战篇
- python 装饰器
- python- 如何return返回多个值
- python基础之 list和 tuple(元组)
- Python迭代器:捕获Generator的返回值
- python的dataframe删除原来索引,重新建立从0开始的索引