栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何从Python的zip文件中的zip文件读取?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从Python的zip文件中的zip文件读取?

当您

.open()
在
ZipFile
实例上使用调用时,您确实会得到一个打开的文件句柄。但是,要 读取
zip文件,
ZipFile
该类需要更多内容。它需要能够在该文件上进行 搜索
,并且
.open()
在您的情况下,返回的对象是不可搜索的。只有Python
3(3.2及更高版本)会生成
ZipExFile
支持搜索的对象(前提是外部zip文件的基础文件句柄是可搜索的,并且没有任何尝试写入该
ZipFile
对象的操作)。


解决方法是读取整个拉链进入使用存储器

.read()
,其存储在一个
BytesIO
对象(一个内存文件 是 可搜索)和进料,为了
ZipFile
:

from io import BytesIO# ...        zfiledata = BytesIO(zfile.read(name))        with zipfile.ZipFile(zfiledata) as zfile2:

或者,在您的示例中:

import zipfilefrom io import BytesIOwith zipfile.ZipFile("parent.zip", "r") as zfile:    for name in zfile.namelist():        if re.search(r'.zip$', name) is not None: # We have a zip within a zip zfiledata = BytesIO(zfile.read(name)) with zipfile.ZipFile(zfiledata) as zfile2:     for name2 in zfile2.namelist():         # Now we can extract         logging.info( "Found internal internal file: " + name2)         print "Processing pre goes here"


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516873.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号