我有一个我想要阅读的文件,它本身是在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)