I want to read a binary in Python, the exact location of which is stored in the binary itself.
The file contains a sequence of two-dimensional arrays with the sizes of the rows and columns of each array, stored as a pair of integers preceding its contents. I want to read all the arrays contained in a file sequentially.
I know this can be done with f = open("myfile", "rb") and f.read(numberofbytes) , but this is pretty awkward because then I will need to convert the output to meaningful data structures. I would like to use numpy np.fromfile with a custom dtype , but did not find a way to read part of the file, leaving it open, and then continue reading with the modified dtype .
I know that I can use os before f.seek(numberofbytes, os.SEEK_SET) and np.fromfile several times, but that would mean a lot of unnecessary jumps in the file.
In short, I want MATLAB fread (or at least something like C ++ ifstream read ).
What is the best way to do this?
jacob source share