码迷,mamicode.com
首页 > 编程语言 > 详细

Python-zip压缩-解压

时间:2016-04-01 12:56:06      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Python-zip压缩-解压

标签:

原文地址:http://www.cnblogs.com/nb-blog/p/5344543.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!