python中使用zipfile模块,实现压缩与解压缩。
代码:
复制代码 代码示例:
import zipfile
### zip file, it will zip file1.txt, file2.txt, file3.txt into new 'filname.zip' file.
f = zipfile.ZipFile('filename.zip', 'w' ,zipfile.ZIP_DEFLATED)
f.write('file1.txt') # Make sure this 3 file already their when do eval.
f.write('file2.txt')
f.write('file3.txt')
f.close()
### extract file from filename.zip
f1=zipfile.ZipFile('filename.zip')
f1.extractall()
f1.close()
python zipfile模块官方链接:
http://docs.python.org/library/zipfile.html