python备份文件

  

转载:http://zhouyangchenrui.iteye.com/blog/682143

python备份文件的例子在windows下采用winrar的rar命令运行可以成功,只是要有winrar软件和设置其环境变量。 

Java代码   收藏代码
  1. # Filename: backup_ver4.py  
  2. import os  
  3. import time  
  4.   
  5. 1.需要备份的文件目录  
  6. source = ['F:\\python\\code']  
  7.   
  8. 2. 备份文件到目标路径  
  9. target_dir = 'F:\\python\\backup\\'  
  10.   
  11. 3. The files are backed up into a zip file.  
  12. 4. The current day is the name of the subdirectory in the main directory  
  13. today = target_dir + time.strftime('%Y%m%d')  
  14. # The current time is the name of the zip archive  
  15. now = time.strftime('%H%M%S')  
  16.   
  17. # Take a comment from the user to create the name of the zip file  
  18. comment = raw_input('Enter a comment -->')  
  19. if len(comment)==0:   
  20.     target = today+os.sep+now+'.zip'  
  21. else:  
  22.     target = today+os.sep+now+'_'+\  
  23.              comment.replace(' ','_')+'.zip'  
  24.     # Notice the backslash!  
  25.   
  26. # Create the subdirectory if it isn't already there  
  27. if not os.path.exists(today):  
  28.     os.mkdir(today)  # make directory  
  29.     print('Successfully created directory', today)  
  30.   
  31. 5. 用winrar的rar命令压缩文件,但首先要安装有winrar且设置winrar到环境变量的路径path中  
  32. zip_command = "rar a %s %s" %(target,''.join(source))  
  33.   
  34. # Run the backup  
  35. #设置winrar到path环境中,这里已经手动添加,如图  
  36. #os.system('set Path=%Path%;C:\Program Files\WinRAR')  
  37. if os.system(zip_command)==0:  
  38.     print('Successful backup to', target)  
  39. else:  
  40.     print('Backup FAILED')  

设置winrar环境变量如下图: 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值