代码如下:
#-*- coding: utf-8 -*-
import os
import time
#step1:需要备份的文件
source='F:\\backuptest\\source\\aa.txt'
#step2:存储备份文件的位置
target_dir='F:\\backuptest\\'
#step3:将备份文件压缩成rar
#step4:当前日期作为目录名
today=target_dir+time.strftime('%Y%m%d')
#step4:当前时间
now=time.strftime('%H%M%S')
#step4:用户注释
comment=raw_input('Enter a comment-->')
if len(comment)==0:
target=today+os.sep+now+'.rar'
else:
target=today+os.sep+now+'_'+\
comment.replace('','_')+'.rar'
#是否需要创建目录
if not os.path.exists(today):
os.mkdir(today)
print "Successfully created directory",today
#step5:使用备份命令
rar_command='"C:\\Program Files\\WinRAR\\WinRAR.exe" A %s %s -r' % (target,source)
# rar_command='"C:\\Program Files\\WinRAR\\WinRAR.exe" A %s %s -r' % (target