python windows下实现备份

来源:互联网 发布:简单的拼图软件 编辑:程序博客网 时间:2024/05/29 17:09

代码如下:

#-*- coding: utf-8 -*-  import osimport 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,source)if os.system(rar_command)==0:  print "Successfully backup to",targetelse:  print "Backup FAILED"  

说明:

1)个人觉得自然字符串容易出问题,所以都使用转义字符

2)备份命令里必须是WinRAR的安装路径,否则命令无法执行

3)备份命令中的‘A’不可缺少,否则虽然显示备份成功 但会找不到备份的文件
 

 

0 0
原创粉丝点击