访次: AmazingCounters.com 次

Python-zip压缩-解压

 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()

 

posted @ 2016-04-01 12:33  IT老登  阅读(348)  评论(0编辑  收藏  举报
访次: AmazingCounters.com 次