import os, zipfile
# @params 文件夹路径,压缩后的文件路径名
def dir_zip(dirname, zipfilename):
filelist = []
if os.path.isfile(dirname):
filelist.append(dirname)
else:
for dirpath, dirnames, filenames in os.walk(dirname):
for name in filenames:
filelist.append(os.path.join(dirpath, name))
zf = zipfile.ZipFile(zipfilename, 'w', zipfile.zlib.DEFLATED)
for tar in filelist:
arcname = tar[len(dirname):]
zf.write(tar, arcname)
zf.close()
if __name__ == '__main__':
dirname = 'E:/mydocument/'
dir_zip(dirname, 'E:/doc.zip')