2

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()
1
  • You should probably check this it might be of help. Commented Mar 8, 2017 at 13:37

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

worth adding that f.read(n_bytes) would read n bytes from byte n if called after the seek

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.