#!/usr/bin/env pythonfrom __future__ import with_statementfrom contextlib import closingfrom zipfile import ZipFile, ZIP_DEFLATEDimport osdef zipdir(basedir, archivename): assert os.path.isdir(basedir) with closing(ZipFile(archivename, "w", ZIP_DEFLATED)) as z: for root, dirs, files in os.walk(basedir): #NOTE: ignore empty directories for fn in files: absfn = os.path.join(root, fn) zfn = absfn[len(basedir)+len(os.sep):] #XXX: relative path z.write(absfn, zfn)if __name__ == '__main__': import sys basedir = sys.argv[1] archivename = sys.argv[2] zipdir(basedir, archivename)
例:
C:zipdir> python -mzipdir c:tmptest test.zip
它创建
'C:zipdirtest.zip'包含目录内容的存档
'c:tmptest'。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)