如何遍历 python 中 zip 存档的 zip 存档?
How to iterate over zip archive of zip archives in python?
有压缩包
zip.zip 与
zip1.zip 和
zip2.zip 在里面。
在 zip1.zip 中是 0.txt,在 zip2.zip 中是 0.txt。
总共 O.txt 是字符串“Hello world”。
如何遍历 zip.zip 来打印:
“你好世界”
“你好世界”
我有,但没用:
from zipfile import ZipFile
with ZipFile(zip.zip, 'r') as zipObj:
listOfFileNames = zipObj.namelist()
for fileName in listOfFileNames:
# Check filename endswith csv
if fileName.endswith('.zip'):
with ZipFile(fileName) as z:
txt = z.read('0.txt')
print (txt)
调试:
with ZipFile(zip.zip, 'r') as zipObj:
File "E:\python\Lib\zipfile.py", line 1265, in __init__
self._RealGetContents()
File "E:\python\Lib\zipfile.py", line 1328, in _RealGetContents
endrec = _EndRecData(fp)
File "E:\python\Lib\zipfile.py", line 264, in _EndRecData
fpin.seek(0, 2)
AttributeError: 'ZipFile' object has no attribute 'seek'
(更新):
预览调试问题是,在生产项目中,我传递的不是存档 zip.zip,而是 list_of_files,没有指向选择第一个文件 (list_of_files[0]):
with ZipFile(list_of_files[0], 'r') as zipObj:
使用io.BytesIO()
将内容提取到内存
from zipfile import ZipFile
import io
with ZipFile('zip.zip', 'r') as zipObj:
for fileName in zipObj.namelist():
if fileName.endswith('.zip'):
content = io.BytesIO(zipObj.read(fileName)) # zip1.zip
with ZipFile(content) as z:
for txt in z.namelist():
if txt.endswith('.txt'):
txt = z.read('0.txt')
print (txt)
有压缩包 zip.zip 与 zip1.zip 和 zip2.zip 在里面。 在 zip1.zip 中是 0.txt,在 zip2.zip 中是 0.txt。 总共 O.txt 是字符串“Hello world”。 如何遍历 zip.zip 来打印: “你好世界” “你好世界”
我有,但没用:
from zipfile import ZipFile
with ZipFile(zip.zip, 'r') as zipObj:
listOfFileNames = zipObj.namelist()
for fileName in listOfFileNames:
# Check filename endswith csv
if fileName.endswith('.zip'):
with ZipFile(fileName) as z:
txt = z.read('0.txt')
print (txt)
调试:
with ZipFile(zip.zip, 'r') as zipObj:
File "E:\python\Lib\zipfile.py", line 1265, in __init__
self._RealGetContents()
File "E:\python\Lib\zipfile.py", line 1328, in _RealGetContents
endrec = _EndRecData(fp)
File "E:\python\Lib\zipfile.py", line 264, in _EndRecData
fpin.seek(0, 2)
AttributeError: 'ZipFile' object has no attribute 'seek'
(更新):
预览调试问题是,在生产项目中,我传递的不是存档 zip.zip,而是 list_of_files,没有指向选择第一个文件 (list_of_files[0]):
with ZipFile(list_of_files[0], 'r') as zipObj:
使用io.BytesIO()
from zipfile import ZipFile
import io
with ZipFile('zip.zip', 'r') as zipObj:
for fileName in zipObj.namelist():
if fileName.endswith('.zip'):
content = io.BytesIO(zipObj.read(fileName)) # zip1.zip
with ZipFile(content) as z:
for txt in z.namelist():
if txt.endswith('.txt'):
txt = z.read('0.txt')
print (txt)