展开全部
代码应该没问题,需要确保target_dir存在。如果运行失败,zip应该也有输出,e5a48de588b662616964757a686964616f31333361306430可以一起贴出来。
下面是我的测试结果(为了测试,目录有所区别,并使用os.makedirs保证target_dir存在),测试平台win7 cygwin:import os
import time
source = ['D:/hello-git']#我知道这里是要拷贝的文件
target_dir = 'D:/tmp/tmp' #用来保存的文件地方
os.makedirs(target_dir) #确保目录存在
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'# 文件名字以及格式
zip_command = "zip -r {0} {1}". format(target, ' '.join(source))
print(zip_command)
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')
输出:
zip -r D:/tmp/tmp/20160720110458.zip D:/hello-git
adding: hello-git/ (452 bytes security) (stored 0%)
adding: hello-git/README (408 bytes security) (stored 0%)
adding: hello-git/test-branch.txt (284 bytes security) (stored 0%)
adding: hello-git/test-edit-after-add.txt (408 bytes security) (deflated 9%)
adding: hello-git/update-master.txt (284 bytes security) (deflated 28%)
adding: hello-git/update-master.txt.orig (284 bytes security) (deflated 38%)
Successful backup to D:/tmp/tmp/20160720110458.zip