Python:以zip格式打开文件而不临时解压缩 [英] Python: Open file in zip without temporarily extracting it

查看:99
本文介绍了Python:以zip格式打开文件而不临时解压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不先解压的情况下打开 zip 存档中的文件?

How can I open files in a zip archive without extracting them first?

我正在使用 pygame.为了节省磁盘空间,我把所有的图片都压缩了.是否可以直接从 zip 文件加载给定的图像?例如:pygame.image.load('zipFile/img_01')

I'm using pygame. To save disk space, I have all the images zipped up. Is it possible to load a given image directly from the zip file? For example: pygame.image.load('zipFile/img_01')

推荐答案

Vincent Povirk 的回答完全行不通;

Vincent Povirk's answer won't work completely;

import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgfile = archive.open('img_01.png')
...

您必须将其更改为:

import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgdata = archive.read('img_01.png')
...

有关详细信息,请阅读 ZipFile 文档 这里.

For details read the ZipFile docs here.

这篇关于Python:以zip格式打开文件而不临时解压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆