#假设存在以下文件夹: D:\keys\miyao.将 keys文件夹中的内容备份
import os
import time
source=[r'D:\keys',r'\miyao']
target_dir=r'D:\\'
today=target_dir+time.strftime('%Y%m%d')
now=time.strftime('%H%M%S')
comment=raw_input("enter a comment-->")
print 'comment :',comment
print len(comment)
if len(comment)==0:#如果条件成立,则target为 日期\时间.zip
target=today+os.sep+now+'.zip'
else:
target=today+os.sep+now+'_'+\
comment.replace(' ','')+'.zip'
print 'target :',target
print 'today:',today
if not os.path.exists(today):
os.mkdir(today)
print 'Successfully create directory',today
zip_command='zip -qr %s %s'%(target,''.join(source)) #zip -r 实现对文件夹的备份
print 'zip_command :',zip_command
if os.system(zip_command)==0:
print 'Successful backup to',target
else:
print 'Backup Failed'