简明python教程backup_ver1.py案例

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}中忘了加空格

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容