1 from zipfile import ZipFile 2 from os import listdir 3 from os.path import isfile,isdir,join 4 5 def addFileIntoZipfile(srcDir,fp): 6 for subpath in listdir(srcDir): 7 subpath=join(srcDir,subpath) 8 if isfile(subpath): 9 fp.write(subpath) #写入文件 10 elif isdir(subpath): 11 fp.write(subpath) #写入文件 12 addFileIntoZipfile(subpath,fp) #递归调用 13 14 def zipCompress(srcDir,desZipfile): 15 fp=ZipFile(desZipfile,mode=‘a‘) #以追加模式打开或创建zip文件 16 addFileIntoZipfile(srcDir,fp) 17 fp.close() 18 19 paths=[r‘/Users/c2apple/Desktop/未命名文件夹 2‘,r‘/Users/c2apple/Desktop/I‘,r‘/Users/c2apple/Desktop/未命名文件夹 3‘] 20 for path in paths: 21 zipCompress(path,‘zWriteFile.zip‘)
时间: 2024-10-14 21:54:34