python在windows下实现备份程序实例

yipeiwu_com6年前Python基础

很多书籍里面讲的Python备份都是在linux下的,而在xp上测试一下也可以执行备份功能,代码都差不多相同,就是到执行打包的时候是不一样的。而且要用到winrar,其他的压缩文件也是一样的。

首先我们要把winrar的路径添加到path里面,这里添加完了要重启机子才有效。
这里要注意:把winrar的路径添加到path里面之后一定要重启,否则path的设定不会起作用,打包就会失败!
 
这里用到得命令是:winrar a xxx.zip xxxx
xxx为任意字符
 
实例代码如下:

#备份脚本,用来备份的 
#Filename:backup_ver1.py 
import os 
import time 
import sys 
#备份的源文件路径 
sourc = ['G://test//test.txt'] 
#备份的文件所放的地方 
target_dir = 'G://' 
#备份文件的名字 
target = target_dir + time.strftime('%Y%m%d%H%M%S')+'.rar' 
#zip_command = "zip -qr '%s' %s" % (target,''.join(sourc)) 
#zip_command = "winrar a /"%s/" %s" % (target,' '.join(sourc)) 
zip_command="winrar a %s %s" %(target,' '.join(sourc)) 
print zip_command 
if os.system(zip_command) == 0: 
  print '打包成功!'+target 
else: 
  print '打包失败!

相关文章

Python 字符串操作方法大全

1、去空格及特殊符号复制代码 代码如下:s.strip().lstrip().rstrip(',')2、复制字符串复制代码 代码如下:#strcpy(sStr1,sStr2)sStr1...

python将excel转换为csv的代码方法总结

python:如何将excel文件转化成CSV格式 import pandas as pd data = pd.read_excel('123.xls','Sheet1',index...

Python原始字符串与Unicode字符串操作符用法实例分析

Python原始字符串与Unicode字符串操作符用法实例分析

本文实例讲述了Python原始字符串与Unicode字符串操作符用法。分享给大家供大家参考,具体如下: #coding=utf8 ''''' 在原始字符串里,所有的字符串都是直接按照...

详解python持久化文件读写

持久化文件读写: f=open('info.txt','a+') f.seek(0) str1=f.read() if len(str1)==0: f1 = open('info...

Python解决线性代数问题之矩阵的初等变换方法

定义一个矩阵初等行变换的类 class rowTransformation(): array = ([[],[]]) def __init__(self,array):...