Search code examples
pythonbinaryfileslarge-data

Reading Parts of Large Binary File in Python


I want to read in a random chunk from a large binary data file in Python and so far I have not found a good solution to my problem.

What I have so far is the following, but it only can read in the first n integers and cannot start somewhere else in the file.

import numpy as np  
#Pick an n here.

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

Also the file in too large to use

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

Solution

  • It's all in the docs.

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

    Open it in binary mode

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

    and then you want to use the seek method,

    f.seek(byte_n)
    

    to start elsewhere.