#!/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()