通过os模块的system()方法调用了系统的rar.exe命令,这个方法会返回一个变量exit_status。
import os import time source = [r‘D:\Work\Python\Demo‘, ‘d:\\work\\linux‘] target_dir = ‘D:\\Work\\backup\\‘ target = target_dir + time.strftime(‘%Y%m%d%H%M%S‘) + ‘.rar‘ zip_command = "rar a %s %s" % (target, ‘ ‘.join(source)) if os.system(zip_command) == 0: print ‘Sucessful backup to‘, target else: print ‘Backup Failed‘