#!/usr/bin/env python
# -*- coding:utf-8 -*- import zipfile def addzip():
f = zipfile.ZipFile('test.zip','w',zipfile.ZIP_DEFLATED)
for i in ["/opt/code/my_code/test_zip/ll.py", "/opt/code/my_code/test_zip/l2.py", "/opt/code/my_code/test_zip/t/新建文件夹/新建文本文档.txt"]:
file = i.split('/')[-1]
f.write(i, file) #这个file是文件名,意思是直接把文件添加到zip没有文件夹层级, f.write(i)这种写法,则会出现上面路径的层级
f.close() if __name__ == '__main__':
addzip()