在 Python 中读取大型二进制文件的部分内容

Reading Parts of Large Binary File in Python

我想从 Python 中的大型二进制数据文件中读取随机块,但到目前为止我还没有找到解决问题的好方法。

到目前为止我有以下内容,但它只能读取前 n 个整数,不能从文件中的其他地方开始。

import numpy as np  
#Pick an n here.

f = open("test2.rd14")
a = np.fromfile(f, dtype = np.uint16, count=int(n))

另外文件太大无法使用

with open("test2.rd14") as file:
filecontent = file.read()

文档里都有。

https://docs.python.org/3.6/tutorial/inputoutput.html

以二进制模式打开它

f = open("test2.rd14", "rb")

然后你想使用seek方法,

f.seek(byte_n)

从别处开始。