在这个代码片段(由另一个人编写)中,self.archive
是一个大文件的路径,raw_file
是作为二进制数据读取的文件内容.
with open(self.archive, "rb") as f:
f.seek(offset)
raw_file = start + f.read(dlen - len(start))
...
f.write(raw_file)
此存档文件包含存储的图像文件,我想访问它们 pygame.image.load
,这需要一个 File 对象。但是我需要做一些类似 pygame.image.load(toVirtualFileObject(raw_file))
的事情(即访问这个归档文件的内容作为 File 对象而不先写入磁盘)。
这可以做到吗?
最佳答案
这就是StringIO
(在 Python 2 中)和 io.BytesIO
in(在 Python 3 中)是为了。
关于python - 将二进制数据视为文件对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16007412/