美文网首页Python语言与信息数据获取和机器学习
第10篇,解决一个问题,运用Python来备份指定文件

第10篇,解决一个问题,运用Python来备份指定文件

作者: ZYiDa | 来源:发表于2017-12-21 11:32 被阅读15次

    通过解决一个问题,来学习解决问题的思路。

    ------ZYiDa弗拉斯基
    思路
    • 需要备份的文件与目录应在一份列表中指定出来
    • 备份必须存储在主备份目录中
    • 备份文件打包压缩成zip文件
    • zip文件名的格式为“当前日期时间 + .zip
    • 我们使用在任何Linux/Unix发行版本中都会提供的标准zip命令进行打包。
      只要有命令行界面,我们就可以使用任何需要用到的压缩和归档命令。

    Debug阶段的代码

    详细的说明都在注释里面了。

    import os
    import time
    
    # 以MacOS下为例,win下面的路径不一样
    # 需要备份的文件的目录路径
    source = ['/Users/a1/Desktop/ZAlertViewDemo']
    
    # 备份的目标路径
    target_dir = '/Users/a1/Desktop/ZAlert'
    
    # 备份的文件名称:'当前日期时间+.zip',文件路径为:目标路径+文件名称
    # time.strftime() 创建日期时间
    # os.sep 根据系统来给出对应的分隔符
    target = target_dir + os.sep + time.strftime('%Y%m%H%M%S') + '.zip'
    
    # 如果备份的目标文件路径不存在,就创建该路径。
    if not  os.path.exists(target_dir):
        os.makedirs(target_dir)
    
    # 使用zip压缩命令来打包压缩文件成zip格式
    # -r选项 用以指定zip命令应该递归地对目录进行压缩工作
    # 也就是说它应该包括所有的子文件夹与其中的文件
    # zipCommand = "zip -r {} {}".format(target, ''.join(source))
    zipCommand = f"zip -r {target} {''.join(source)}"
    
    # 输出压缩过程的信息
    print('zipCommand is :')
    print(zipCommand)
    print('Running:')
    
    # 处理压缩的结果
    if os.system(zipCommand) == 0:
        print('Successful backup to :',target)
    else:
        print('Backup failed.')
    

    输出信息

    zipCommand is :
    zip -r /Users/a1/Desktop/ZAlert/201712100550.zip /Users/a1/Desktop/ZAlertViewDemo
    Running:
      adding: Users/a1/Desktop/ZAlertViewDemo/podfile (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/README.md (deflated 12%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/.DS_Store (deflated 93%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/AppDelegate.h (deflated 23%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/AppDelegate.m (deflated 58%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Assets.xcassets/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Assets.xcassets/AppIcon.appiconset/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Assets.xcassets/AppIcon.appiconset/Contents.json (deflated 84%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Base.lproj/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Base.lproj/LaunchScreen.storyboard (deflated 57%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Base.lproj/Main.storyboard (deflated 71%)
    Successful backup to : /Users/a1/Desktop/ZAlert/201712100550.zip
    
    Process finished with exit code 0
    
    

    Maintenance维护阶段

    维护阶段的需求更改

    • 用日期做文件夹名称 ,在该目录下存储当天的备份,这个文件夹在主备份目录下。

    请看下面的代码

    import os
    import time
    
    # 需要备份的文件路径
    source = ['/Users/a1/Desktop/ZAlertViewDemo']
    
    # 主备份目录
    target_main_dir = '/Users/a1/Desktop/BackpackTestFile'
    if  not os.path.exists(target_main_dir):
        os.makedirs(target_main_dir)
    
    # 将当前日期作为当前主备份目录下的总目录
    today = target_main_dir + os.sep + time.strftime('%Y%m%d')
    if not  os.path.exists(today):
        os.makedirs(today)
        print("File path create Successful!")
    
    # 获取当前时间,转化为字符串,作为备份文件的名称
    now = time.strftime('%H%M%S')
    
    # 备份文件的'路径+文件名'
    file_target = today + os.sep +now + '.zip'
    
    # 调用zip命令
    zipCommand = "zip -r {} {}".format(file_target,''.join(source))
    
    #输出压缩过程信息
    print("Zip command is :")
    print(zipCommand)
    print("Start Running!")
    
    # 判断压缩的结果信息
    if os.system(zipCommand) == 0:
        print("Successful backpack file to path:",file_target)
    else:
        print("Filed backpack.")
    
    Zip command is :
    zip -r /Users/a1/Desktop/BackpackTestFile/20171221/102959.zip /Users/a1/Desktop/ZAlertViewDemo
    Start Running!
    
      adding: Users/a1/Desktop/ZAlertViewDemo/.git/objects/info/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/.git/objects/pack/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/.git/ORIG_HEAD (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/.git/refs/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/.git/refs/heads/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/.git/refs/heads/master (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/.git/refs/remotes/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/.git/refs/remotes/origin/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/.git/refs/remotes/origin/master (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/.git/refs/remotes/origin2/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/.git/refs/remotes/origin2/master (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/.git/refs/tags/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/.git/sourcetreeconfig (deflated 40%)
      adding: Users/a1/Desktop/ZAlertViewDemo/LICENSE (deflated 41%)
      adding: Users/a1/Desktop/ZAlertViewDemo/podfile (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/README.md (deflated 12%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/.DS_Store (deflated 93%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/AppDelegate.h (deflated 23%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/AppDelegate.m (deflated 58%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Assets.xcassets/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Assets.xcassets/AppIcon.appiconset/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Assets.xcassets/AppIcon.appiconset/Contents.json (deflated 84%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Base.lproj/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Base.lproj/LaunchScreen.storyboard (deflated 57%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Base.lproj/Main.storyboard (deflated 71%)
    Successful backpack file to path: /Users/a1/Desktop/BackpackTestFile/20171221/102959.zip
    
    Process finished with exit code 0
    
    

    Bug Fixing 错误修复

    下面的代码看似很正确,但是却不能正常运行。不过我们可以从下面学习到解决问题的思路

    import os
    import time
    
    source = ['/Users/a1/Desktop/ZAlertViewDemo']
    
    tatget_main_dir = '/Users/a1/Desktop/backup_test2'
    if not os.path.exists(tatget_main_dir):
        os.makedirs(tatget_main_dir)
    
    today = tatget_main_dir + os.sep + time.strftime('%Y%m%d')
    if not os.path.exists(today):
        os.makedirs(today)
    
    now = time.strftime('%H%M%S')
    
    comment = input('请输入文件名前缀:')
    if len(comment) == 0:
        file_taget_dir = tatget_main_dir + today + os.sep + now + '.zip'
    else:
        file_taget_dir = tatget_main_dir + today + os.sep + comment + '_' + now + '.zip'
    
    zipCommand = "zip -r {} {}".format(file_taget_dir,''.join(source))
    
    print("Zip conmand is ",zipCommand)
    print("Start Running!")
    print(zipCommand)
    
    if os.system(zipCommand) == 0:
        print("Successful backup to path :",file_taget_dir)
    else:
        print("Backup Filed.")
    

    运行报错,控制台输出结果

    请输入文件名前缀:ert
    Zip conmand is  zip -r /Users/a1/Desktop/backup_test2/Users/a1/Desktop/backup_test2/20171221/ert_110054.zip /Users/a1/Desktop/ZAlertViewDemo
    Start Running!
    zip -r /Users/a1/Desktop/backup_test2/Users/a1/Desktop/backup_test2/20171221/ert_110054.zip /Users/a1/Desktop/ZAlertViewDemo
    zip I/O error: No such file or directory
    zip error: Could not create output file (/Users/a1/Desktop/backup_test2/Users/a1/Desktop/backup_test2/20171221/ert_110054.zip)
    Backup Filed.
    
    Process finished with exit code 0
    

    从上面的输出信息可以看到,错误信息是找不到文件或者文件夹信息。
    经过一步一步排查,发现问题出现在下面的区域

    if len(comment) == 0:
        file_taget_dir = tatget_main_dir + today + os.sep + now + '.zip'
    else:
        file_taget_dir = tatget_main_dir + today + os.sep + comment + '_' + now + '.zip'
    

    因为在设置备份文件路径时,我们多加了一个tatget_main_dir造成了路径错误,从而无法进行备份。
    去除掉上面代码中的tatget_main_dir +,再次运行就正常了,
    请看正常后的输出信息

    请输入文件名前缀:qwert
    Zip conmand is  zip -r /Users/a1/Desktop/backup_test2/20171221/qwert_110447.zip /Users/a1/Desktop/ZAlertViewDemo
    Start Running!
    zip -r /Users/a1/Desktop/backup_test2/20171221/qwert_110447.zip /Users/a1/Desktop/ZAlertViewDemo
      adding: Users/a1/Desktop/ZAlertViewDemo/podfile (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/README.md (deflated 12%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/.DS_Store (deflated 93%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/AppDelegate.h (deflated 23%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/AppDelegate.m (deflated 58%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Assets.xcassets/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Assets.xcassets/AppIcon.appiconset/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Assets.xcassets/AppIcon.appiconset/Contents.json (deflated 84%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Base.lproj/ (stored 0%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Base.lproj/LaunchScreen.storyboard (deflated 57%)
      adding: Users/a1/Desktop/ZAlertViewDemo/ZAlertViewDemo/Base.lproj/Main.storyboard (deflated 71%)
    Successful backup to path : /Users/a1/Desktop/backup_test2/20171221/qwert_110447.zip
    
    Process finished with exit code 0
    

    关于zip命令的两个说明

    • zipCommand = "zip -r -v {} {}".format(file_taget_dir,''.join(source)),在zip命令中添加-v能更加详细的显示压缩的过程进行信息。
    • zipCommand = "zip -r -q {} {}".format(file_taget_dir,''.join(source))则能使程序静默进行,不显示输出压缩过程的进行信息。

    总结-软件开发的流程

    由上面处理问题的思路,可以总结一下软件开发的流程

    • What/做什么 (分析)
    • How/怎么做 (设计)
    • Do It/开始做 (执行)
    • Test/测试 (测试与修复错误)
    • Use/使用 (操作或开发)
    • Maintain/维护(改进)
    未命名文件.png

    相关文章

      网友评论

        本文标题:第10篇,解决一个问题,运用Python来备份指定文件

        本文链接:https://www.haomeiwen.com/subject/ojpowxtx.html