I want to read a binary file in Python, the exact layout of which is stored in the binary file itself.
The file contains a sequence of two-dimensional arrays, with the row and column dimensions of each array stored as a pair of integers preceding its contents. I want to successively read all of the arrays contained within the file.
I know this can be done with f = open("myfile", "rb") and f.read(numberofbytes), but this is quite clumsy because I would then need to convert the output into meaningful data structures. I would like to use numpy's np.fromfile with a custom dtype, but have not found a way to read part of the file, leaving it open, and then continue reading with a modified dtype.
I know I can use os to f.seek(numberofbytes, os.SEEK_SET) and np.fromfile multiple times, but this would mean a lot of unnecessary jumping around in the file. 
In short, I want MATLAB's fread (or at least something like C++ ifstream read).
What is the best way to do this?