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()
Community
  • 1
  • 1
Rob Schneider
  • 165
  • 5
  • 16
  • You should probably check [this](http://stackoverflow.com/questions/1035340/reading-binary-file-in-python-and-looping-over-each-byte) it might be of help. – YGouddi Mar 08 '17 at 13:37

1 Answers1

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.

Denziloe
  • 7,473
  • 3
  • 24
  • 34