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