美文网首页
简明python教程backup_ver1.py案例

简明python教程backup_ver1.py案例

作者: 黑Gc | 来源:发表于2017-06-15 22:44 被阅读68次

    import os

    import time

    source = [' /Users/Tblacker/Documents/Test']

    target_dir ='/Users/Tblacker/Documents/Backup'

    target = target_dir + os.sep + \

    time.strftime('%Y%m%d%H%M%S') +'.zip'

    if not os.path.exists(target_dir):

        os.mkdir(target_dir)

    zip_command ='zip -qr {0}{1}'.format(target,' '.join(source))

    print('Zip command is:')

    print(zip_command)

    print('Running:')

    if os.system(zip_command) ==0:

        print('Successful backup to',target)

    else:

        print('Backup FAILED')


    以上是按照简明python教程写的代码

    因为一个空格问题,出现以下问题:

    Zip command is:

    zip -qr /Users/Tblacker/Documents/Backup/20170615224248.zip/Users/Tblacker/Documents/Test

    Running:

    zip error: Nothing to do! (/Users/Tblacker/Documents/Backup/20170615224248.zip/Users/Tblacker/Documents/Test.zip)

    Backup FAILED

    Process finished with exit code 0


    通过在

    source = [' /Users/Tblacker/Documents/Test']

    的/User前面添加空格

    终于成功了,但这是为什么呢?等明白了再补完


    原来是

    zip_command ='zip -qr {0}{1}'.format(target,' '.join(source))

    忘了在{0}{1}中忘了加空格

    相关文章

      网友评论

          本文标题:简明python教程backup_ver1.py案例

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