我有一个文件,想要读取它,该文件本身被压缩在zip归档文件中。例如,parent.zip包含child.zip,而child.zip包含child.txt。我无法读取child.zip。请问有人能够更正我的代码吗?
我推测我需要将child.zip创建为类似文件的对象,然后使用第二个zipfile实例打开它,但是由于刚学习Python,我的zipfile.ZipFile(zfile.open(name))很蠢。它会导致zipfile.BadZipfile异常:“文件不是zip文件”,尽管(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"