python windows下实现备份

本文介绍了如何在Python环境下,利用WinRAR在Windows操作系统中执行备份操作。重点强调了在构建备份命令时需要注意使用转义字符,确保WinRAR的完整路径,并且指出'А'参数对于成功创建备份文件的重要性。
摘要由CSDN通过智能技术生成

代码如下:

#-*- 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值