Reading a binary file with python
Reading a binary file with python
Read the binary file content like this:
with open(fileName, mode=rb) as file: # b is important -> binary
fileContent = file.read()
then unpack binary data using struct.unpack:
The start bytes: struct.unpack(iiiii, fileContent[:20])
The body: ignore the heading bytes and the trailing byte (= 24); The remaining part forms the body, to know the number of bytes in the body do an integer division by 4; The obtained quotient is multiplied by the string i
to create the correct format for the unpack method:
struct.unpack(i * ((len(fileContent) -24) // 4), fileContent[20:-4])
The end byte: struct.unpack(i, fileContent[-4:])
In general, I would recommend that you look into using Pythons struct module for this. Its standard with Python, and it should be easy to translate your questions specification into a formatting string suitable for struct.unpack()
.
Do note that if theres invisible padding between/around the fields, you will need to figure that out and include it in the unpack()
call, or you will read the wrong bits.
Reading the contents of the file in order to have something to unpack is pretty trivial:
import struct
data = open(from_fortran.bin, rb).read()
(eight, N) = struct.unpack(@II, data)
This unpacks the first two fields, assuming they start at the very beginning of the file (no padding or extraneous data), and also assuming native byte-order (the @
symbol). The I
s in the formatting string mean unsigned integer, 32 bits.
Reading a binary file with python
To read a binary file to a bytes
object:
from pathlib import Path
data = Path(/path/to/file).read_bytes() # Python 3.5+
To create an int
from bytes 0-3 of the data:
i = int.from_bytes(data[:4], byteorder=little, signed=False)
To unpack multiple int
s from the data:
import struct
ints = struct.unpack(iiii, data[:16])