【发布时间】:2018-12-02 10:11:06
【问题描述】:
我使用以下方法从二进制文件中的任何给定偏移量读取二进制数据。我拥有的二进制文件有 10GB 大,所以我通常会在需要时读取其中的一部分,方法是指定我应该从哪个偏移量start_read 以及要读取多少字节num_to_read。我使用Python 3.6.4 :: Anaconda, Inc.、平台Darwin-17.6.0-x86_64-i386-64bit和os模块:
def read_from_disk(path, start_read, num_to_read, dim):
fd = os.open(path, os.O_RDONLY)
os.lseek(fd, start_read, 0) # Where to (start_read) from the beginning 0
raw_data = os.read(fd, num_to_read) # How many bytes to read
C = np.frombuffer(raw_data, dtype=np.int64).reshape(-1, dim).astype(np.int8)
os.close(fd)
return C
当要读取的数据块大约小于 2GB 时,此方法非常有效。当num_to_read > 2GG,我得到这个错误:
raw_data = os.read(fd, num_to_read) # How many to read (num_to_read)
OSError: [Errno 22] Invalid argument
我不确定为什么会出现此问题以及如何解决它。非常感谢任何帮助。
【问题讨论】:
-
你在什么平台上?
-
你使用的是什么版本的 Python?
-
听起来你用的是32位软件,需要64位才能访问超过2GB。
-
@abarnert Mac OSX。
-
@Barmar 不,他可能在 64 位平台上,但使用 32 位文件 API。
标签: python python-3.x operating-system