Home >Backend Development >Python Tutorial >python在windows下实现备份程序实例

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-16 08:43:281288browse

很多书籍里面讲的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 '打包失败!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn