我有一个大(21 GB)文件,我想将其读入内存,然后传递给一个子例程,该子例程对我透明地处理数据。我在 Centos 6.5 上使用 python 2.6.6,因此无法升级操作系统或 python。目前我正在使用
f = open(image_filename, "rb")
image_file_contents=f.read()
f.close()
transparent_subroutine ( image_file_contents )
速度很慢(约 15 分钟)。在开始读取文件之前,我知道文件有多大,因为我调用 os.stat( image_filename ).st_size
所以如果有意义的话我可以预先分配一些内存。
谢谢
最佳答案
使用发电机
def generator(file_location):
with open(file_location, 'rb') as entry:
for chunk in iter(lambda: entry.read(1024 * 8), b''):
yield chunk
go_to_streaming = generator(file_location)
关于python - python读取大型二进制文件最有效的方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25754837/