python拷贝文件

来源:互联网 发布:网络基础架构的理解 编辑:程序博客网 时间:2024/05/14 22:36
import os, os.path, shutil, time, datetime#copy file from one dir to anotherdef 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 dirdef 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 filedef writeVersionInfo(targetFile):    open(targetFile, "wb").write("Revison:")#get current timedef 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#MainTarget_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"

0 0
原创粉丝点击