pythonbyte

How to read only bytes in rb mode in python


I'm reading a binary file, but bytes that can be read as characters are being read as characters. this byte character is merged with the previous byte, two or more characters in a row are merged in the same byte

#first 24 bytes in the file
\x01\xda\x01\x01\x00\x03\x04\xb0\x02v\x00\x03\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00
#                                   ↑
#                      byte being read as character
#more bytes later in the file
\x01u"\x00\x01\x83U\x00\x01\x91\x88\x00\x01\x9f\xbb\x00\x01\xad\xe6\x00\x01\xbc\x14\x00\x01\xcaB\x00\x01\xd8r\x00\x01\xe6\
                  ↑             ↑
#           bytes with 2 extra characters

I have no idea how to fix this

#code that reads the file
f = open('tree.sgi', 'rb')
byteAdressEnd = 24
byte = str(f.read(byteAdressEnd))

Solution

  • That is just how python prints bytes. If you print the value of each byte you will find out it is the correct value. If you want it to print all characters as hex values (so as you want it to) you can use this function to print it:

    def printBytes(data: bytes):
        for b in data:
            print(f'\\{hex(b)[1:]}', end="")
        print()