您的位置:首页 > 编程语言 > Python开发

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"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: