My target is to read a binary file using mmap() and a class. The issue is that the data I want to get from the file is not in byte position 0, and the offset is 24. If I set this offset to the mmap function mmap.mmap(fd, length, access, offset = 24)
an error is raised, since offset has to be a multiple of granularity. My code is:
class StructHeader(Structure):
_pack_ = 1
_fields_ = [('nothing', c_char*24),('v1', c_ubyte),('v2', c_ubyte)]
d_arrayHeader = StructHeader*1
if __name__ == '__main__':
fd = os.open(filePath, os.O_RDWR)
granularity = mmap.ALLOCATIONGRANULARITY
mmap_file = mmap.mmap(fd, length=187, access=mmap.ACCESS_WRITE, offset=0)
data = d_arrayHeader.from_buffer(mmap_file)
i = data[0]
print i.v1, i.v2
I thought three solutions:
data = d_arrayHeaderLAS.from_buffer(mmap_file[24:])
where the argument of from_buffer() is a subarray of mmap. The issue is that this subarray is transformed from a mmap object to str, and it doesn't work.struct.unpack()
, but I wouldn't like to use it, because it is slower.I'd like to know if there is an easier way to get a mmap with an offset. This case I showed is easy, but I'd like to use it in more complicated cases, such as getting data from binary file where the offset is equal to the header. In these cases, header and data structures are differents, and I should use different classes. And I need it is fast, since files are too big.
Thank you.
You can use "mmap_file.seek(0)" to make file pointer at offset zero ...