小编Mic*_*son的帖子

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

我有一个我想要阅读的文件,它本身是在zip存档中压缩的.例如,parent.zip包含child.zip,其中包含child.txt.我在阅读child.zip时遇到了麻烦.谁能纠正我的代码?

我假设我需要创建一个类似文件的对象的child.zip,然后用第二个zipfile实例打开它,但是对于python我是新的zipfile.ZipFile(zfile.open(name))是愚蠢的.它引发了一个zipfile.BadZip文件:"文件不是一个zip文件"on(独立验证)child.zip

import zipfile
with 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
            with **zipfile.ZipFile(zfile.open(name))** as zfile2:
                    for name2 in zfile2.namelist():
                        # Now we can extract
                        logging.info( "Found internal internal file: " + name2)
                        print "Processing code goes here"
Run Code Online (Sandbox Code Playgroud)

python zip file unzip

33
推荐指数
2
解决办法
2万
查看次数

标签 统计

file ×1

python ×1

unzip ×1

zip ×1