我说的cmd shell 是windows 的类似于dos的命令窗口。
下面的结果是我用本机7z压缩的结果。>>> import time
>>> import os
>>> def test():
source = [r'c:log']
target_dir = r'd:\log'
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.7z'
rar_command = '7z a %s %s ' %(target,''.join(source))
print rar_command
if os.system(rar_command) == 0 or os.system(rar_command) == 1:
print "Success"
else:
print "Fail"
>>> test()
7z a d:\log20150429141941.7z c:log
Success
>>>
d盘下会生成log20150429141941.7z文件。
此处有个问题,就是os.system的返回值是1,不是0,但是备份已经成功了。
你可以在你的脚本里面加上or os.system(rar_command) == 1:
看看打印的是成功还是失败。