在windons下写一个zip备份的python脚本

info-zip下载地址:Info-ZIP's SourceForge site. http://sourceforge.net/projects/infozip/files/


如下图所示,下载红线圈住的内容

转--windows系统上使用Python脚本实现备份文件借用info_zip



下载完毕后安装,记住安装目录,安装完成后打开安装目录下的Wiz.exe,目的是执行下面代码中zip_command。(window中没有Unix下的zip命令,所以要下载info_zip代替)

转--windows系统上使用Python脚本实现备份文件借用info_zip

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import os
import time
#1.需要备份的文件或者文件夹
source = [ 'd:\\sll' , 'D:\\download' #需要注意python下\为转义字符,所以要写\\
#2.需要备份的存放目录
target_dir = 'd:\\pyback\\'
#3要压缩的文件
target = target_dir + time.strftime( '%Y%m%d%H%M%S' ) + '.zip'
print target
zip_command = "zip -qr %s%s" % (target, '' .join(source))
print zip_command
if os.system(zip_command) = = 0 :
     print 'Successfull backto' ,target
else :
     print 'backup failed'

同时需要注意的是这里的压缩文件命令zip_command第一个%S 不需要加上单引号('%s'),

XXX zip_command ="zip -qr '%s' %s" % (target,''.join(source))这是错误的

这样windows下无法创建文件。

zip_command= "zip -qr %s %s" % (target,''.join(source))这是正确的

然后运行 “python back.py”压缩成功info-zip