1 windows环境
2已安装winrar软件
3将winrar.exe拷贝到系统的system32目录下
4压缩的源文件D:/WORK
5目标文件位于D盘根目录下,名称为当前的时间日期.rar
6 附上winrar其他指令 http://www.docin.com/p-274023603.html&endPro=true
import os import time source = r'D:\work' target_dir = r'D:\\' target = target_dir + time.strftime('%Y%m%d%H%M%S')+'.rar' rar_command = "winrar a {0} {1}".format(target, source) if os.system(rar_command) ==0: print("成功") else: print("失败")
优化方案一、
import os import time source = r'D:\work' target_dir = r'D:\\' today = target_dir + time.strftime('%Y%m%d') now = time.strftime('%H%M%S') if not os.path.exists(today): os.mkdir(today) # make directory print( 'Successfully created directory', today) target = today + os.sep + now + '.rar' rar_command = "winrar a {0} {1}".format(target, source) if os.system(rar_command) ==0: print("成功") else: print("失败")
优化方案二、
import os import time source = r'D:\work' target_dir = r'D:\\' today = target_dir + time.strftime('%Y%m%d') now = time.strftime('%H%M%S') comment = input('Enter a comment --> ') if len(comment) == 0: # check if a comment was entered target = today + os.sep + now + '.rar' else: target = today + os.sep + now + '_' +\ comment.replace(' ', '_') + '.rar' if not os.path.exists(today): os.mkdir(today) # make directory print( 'Successfully created directory', today) rar_command = "winrar a {0} {1}".format(target, source) if os.system(rar_command) ==0: print("成功") else: print("失败")