Вы можете использовать «bytearray»:
ba=bytearray(1000) # zero-filled
In: sys.getsizeof(ba)
Out: 1057
In: ba[0:10]
Out: bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
In: ba[5]=255
In: ba[0:10]
Out: bytearray(b'\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00')
Редактировать:
with open("data","wb") as ff:
ff.write(int.to_bytes(100,1,"big"))
ff.write(int.to_bytes(300,2,"big"))
ff.write(int.to_bytes(5001,4,"big"))
ba=bytearray(7)
ba
Out: bytearray(b'\x00\x00\x00\x00\x00\x00\x00')
with open("data","rb") as ff:
ba[0:7]= ff.read(7)
ba
Out: bytearray(b'd\x01,\x00\x00\x13\x89')
int.from_bytes(ba[0:1],"big")
Out: 100
int.from_bytes(ba[1:3],"big")
Out: 300
int.from_bytes(ba[3:],"big")
Out: 5001