zoukankan      html  css  js  c++  java
  • python 备份文件

     config_BK.txt

    Admin.Validation/Admin.Validation.csproj
    BusinessObjects/CandidateManagementBO.cs        
    BusinessObjects\EmpTempDataBO.cs          
    BusinessObjects\JobAdBO.cs          
    BusinessObjects\JobApplicationBO.cs          
    BusinessObjects\PurchaseOnlineBO.cs          
    BusinessObjects\ResumeRequestBO.cs          
    BusinessObjects\ResumeRequestResponseBO.cs          
    BusinessObjects\TrialEmployerBO.cs          
    Common.Tier\EmailTemplateHelper.cs          
    Resources\Wordings.designer.cs          
    Resources\Wordings.resx          

    PythonBK.bat

    @echo off

    ::python Python_CopyFile.py %*

    Set configPath=config_BK.txt
    Set sourceDir=C:\src\v8\src\
    Set targetDir=D:\workspace\BKFiles\EmployerEmail\
    Set isComment=False
    Set isAddToday=True

    python Python_CopyFile.py %configPath% %sourceDir% %targetDir% %isComment% %isAddToday%

    pause & exit

    config_DLL.txt

    C:\src\v8\src\Core\bin\Debug\Core.dll
    C:\src\v8\src\Core\bin\Debug\Core.pdb
    C:\src\v8\src\Core\bin\Debug\Core.XML
    
    C:\src\v8\src\Metaform\bin\Debug\Metaform.dll
    C:\src\v8\src\Metaform\bin\Debug\Metaform.pdb
    
    C:\src\v8\src\Resources\bin\Debug\Resources.dll
    C:\src\v8\src\Resources\bin\Debug\Resources.pdb
    C:\src\v8\src\Resources\bin\Debug\Resources.XML

    config_DLL.bat

    @echo off
    
    ::python Python_CopyFile.py %*
    
    Set configPath=config_DLL.txt
    Set sourceDir=""
    Set targetDir=C:\src\v8\src\Web\bin\
    Set isComment=False
    Set isAddToday=False
    
    python Python_CopyFile.py %configPath% %sourceDir% %targetDir% %isComment% %isAddToday%
    
    pause & exit

    Python_CopyFile.py

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-  
    #coding=utf-8  
    #copy files
    import os, sys
    import shutil
    import time
    
    configPath = "D:\web\configBK.txt"            #保存需要拷贝的文件路径
    sourceDir = "C:\\src\\v8\\src\\"    #需要拷贝的文件的共用路径 sourceDir为空时表示configPath文本里的是完整路径
    targetDir = "D:\\web\\"                        #保存路径targetDir\today\...
    isComment = "False"                            #是否需要加入说明targetDir\today_说明\...
    isAddToday = "False"                            #是否需要在备份路径中增加一个today日期的文件夹
    filterFileTypes = [".csproj", ".vsmdi", ".bat"]    #需要过滤的文件类型
    
    #拷贝文件的函数
    def copyFile(sourceFile, targetFile):
        if os.path.isfile(sourceFile):                                #判断待复制的文件是否存在
            try:
                '''
                #方法一
                index = targetFile.rfind("/")                        #目标路径中最后一个/的位置   i.e. c:/path
                if index == -1:                                        #如果不存在/, 那么就是用\       i.e. c:\path
                    ext_dir = targetFile[:targetFile.rfind("\\")]    #得到文件所在的文件夹路径   
                else:
                    ext_dir = targetFile[:targetFile.rfind("/")]    #得到文件所在的文件夹路径   
                '''
                #方法二
                targetFile = targetFile.replace('\\', '/')            #替换路径中的 \ 为 /            
                #ext_dir = os.path.dirname(targetFile)                #得到文件所在的文件夹路径
                ext_dir = os.path.split(targetFile)[0]                #os.path.split(path) 把路径分成文件夹路径和文件名
    
                if not os.path.exists(ext_dir):                        #如果文件夹不存在就创建文件夹
                    os.makedirs(ext_dir)
    #            else:
    #                print(ext_dir)
    
                shutil.copy(sourceFile, targetFile)                    #开始拷贝文件
    
                print('copied ' + sourceFile + '\n')
            except:
                print('copied except from ' + sourceFile + '\nto ' + targetFile + '\n')
        else:
            print('not exists file %s' % sourceFile)
    
    #获得所有待拷贝的文件和目标路径
    def BKFiles(configPath, sourceDir, targetDir, isComment = "False", isAddToday = "False", filterFileTypes = []):
        fileObject = open(configPath)                                        #打开存放路径的文本
        fileList = []
        try:
            for line in fileObject:
                fileList.append(line.strip('\n'))                            #把所有路径加入到list中,并去读取文本行后面的符号\n
        finally:
            fileObject.close()
        
        if isAddToday == "True":
            today = time.strftime('%Y%m%d')                                        #得到今天的日期
            if isComment == "True":                                                #是否需要给备份加入注释 
                comment = raw_input("Enter a comment:")                            #提示输入注释
                if len(comment) == 0:
                    todayDir = targetDir + today + "\\"                            #生成备份文件夹名  i.e. 20130510
                else:
                    todayDir = targetDir + today + "_" + comment + '\\'            #生成备份文件夹名  i.e. 20130510_comment
            else:
                todayDir = targetDir + today + "\\"
        else:
            todayDir = targetDir
    
    
        if not os.path.exists(todayDir):                                    #如果文件夹不存在就创建文件夹
            os.makedirs(todayDir)
    #        print('Successful created directory')
    
        count = 0
        for f in fileList:                                                #循环拷贝文件
            #print("--%s--" % f)
            if f != "":
                isFilter = False
                for fileType in filterFileTypes:                                #循环需要过滤的文件类型
                    #if f.find(fileType) > 0:                                    #判断文件是否是要过滤的类型
                    if os.path.splitext(f)[1] == fileType:                        #os.path.splitext(filename) 把文件名分成文件名称和扩展名 
                        isFilter = True
                        break    
                if not isFilter:
                    try:
                        if sourceDir == "":
                            copyFile(sourceDir + f, todayDir)
                        else:
                            copyFile(sourceDir + f, todayDir + f)
                        count = count + 1
                    except:
                        pass
                else:
                    print("filtered " + f + "\n")
        print("copied %d file(s)\n" % count)
    
    if __name__ == "__main__":                                                #如果不是import此python,那么开始备份文件
        args = sys.argv[1:]
        if not args:
            print("not args")
            args = [configPath, sourceDir, targetDir, isComment, isAddToday]
    
        configPath = args[0]
        sourceDir = args[1]
        targetDir = args[2]
        isComment = args[3]
        isAddToday = args[4]
    
        print("Paramaters:")
        print("configPath:" + configPath + ".")
        print("sourceDir:" + sourceDir + ".")
        print("targetDir:" + targetDir + ".")
        print("isComment:" + isComment + ".")
        print("isAddToday:" + isAddToday + ".\n")
    
        BKFiles(configPath, sourceDir, targetDir, isComment, isAddToday, filterFileTypes)
  • 相关阅读:
    自适应图形的绘制
    红色椭圆、蓝色正方形、黄色三角形、紫色五角星
    [2020牛客寒假算法基础集训营2]G-判正误
    [2020牛客寒假算法基础集训营1]I-nico和niconiconi
    [2020牛客寒假算法基础集训营1]H-nozomi和字符串
    [2020牛客寒假算法基础集训营1]A-honoka和格点三角形
    约瑟夫环以及其变种集合
    unsign long long 与 long long
    【POJ-3279】Fliptile
    B--Bookshelf 2
  • 原文地址:https://www.cnblogs.com/dfg727/p/3070492.html
Copyright © 2011-2022 走看看