from zipfile import ZipFile
from os import listdir
from os.path import isfile,isdir,join def addFileIntoZipfile(srcDir,fp):
for subpath in listdir(srcDir):
subpath=join(srcDir,subpath)
if isfile(subpath):
fp.write(subpath) #写入文件
elif isdir(subpath):
fp.write(subpath) #写入文件
addFileIntoZipfile(subpath,fp) #递归调用 def zipCompress(srcDir,desZipfile):
fp=ZipFile(desZipfile,mode='a') #以追加模式打开或创建zip文件
addFileIntoZipfile(srcDir,fp)
fp.close() paths=[r'/Users/c2apple/Desktop/未命名文件夹 2',r'/Users/c2apple/Desktop/I',r'/Users/c2apple/Desktop/未命名文件夹 3']
for path in paths:
zipCompress(path,'zWriteFile.zip')