zipfile 模块用于在python 中做压缩、解压缩。
下面是一个例子:
#/usr/bin/python3
import os
import time
import zipfile
source_dir = "/home/guilan/development/algorithm"
filename = "/home/guilan/development/python/example/"+time.strftime('%Y%m%d')+'.zip'
target = zipfile.ZipFile(filename, 'w')
if os.path.isdir(source_dir):
for d in os.listdir(source_dir):
target.write(source_dir + os.sep + d)
target.close()
上面的例子把 /home/guilan/development/algorithm 这个文件夹压缩一下,存放在 /home/guilan/development/python/example 下面。