Treat binary data as a file object?

本秂侑毒 提交于 2020-01-30 04:31:16

问题


Here is the important part of some existing code that I'm trying to adapt for my own uses.

The notable part is that self.archive leads to a massive file and raw_file is binary data extracted (painfully) from this giant file.

with open(self.archive, "rb") as f:
    f.seek(offset)
    raw_file = start + f.read(dlen - len(start))
    ...
    f.write(raw_file)

The existing code extracts the contents of an archive-like file to disk, but I need to only read these stored files from this archive (if that makes any sense).

I need to use Pygame to read a few hundred mbs of data from this file, mostly as images, using methods such as pygame.image.load(). Rather than having this code write all of the file's contents to disk and then re-reading it again in "not-binary", I'd like to directly do something like pygame.image.load(toVirtualFileObject(raw_file)). Does anyone know of something like this?


回答1:


This is what StringIO (in Python 2) and io.BytesIO in (in Python 3) are for.



来源:https://stackoverflow.com/questions/16007412/treat-binary-data-as-a-file-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!