1 #打包成zip文件 2 import zipfile 3 f = zipfile.ZipFile('archive.zip','w',zipfile.ZIP_DEFLATED) 4 f.write('file_to_add.py') 5 f.close() 6 7 #从zip文件解包 8 import zipfile 9 zfile = zipfile.ZipFile('archive.zip','r') 10 for filename in zfile.namelist(): 11 data = zfile.read(filename) 12 file = open(filename, 'w+b') 13 file.write(data) 14 file.close() 15 16 #把整个文件夹内的文件打包 17 import zipfile 18 f = zipfile.ZipFile('archive.zip','w',zipfile.ZIP_DEFLATED) 19 startdir = "c:\mydirectory" 20 for dirpath, dirnames, filenames in os.walk(startdir): 21 for filename in filenames: 22 f.write(os.path.join(dirpath,filename)) 23 f.close()