Skip to content Skip to sidebar Skip to footer

Reading And Interpreting Data From A Binary File In Python

I want to read a file byte by byte and check if the last bit of each byte is set: #!/usr/bin/python def main(): fh = open('/tmp/test.txt', 'rb') try: byte = fh.rea

Solution 1:

Try using the bytearray type (Python 2.6 and later), it's much better suited to dealing with byte data. Your try block would be just:

ba = bytearray(fh.read())
forbytein ba:
    printbyte & 1

or to create a list of results:

low_bit_list = [byte & 1forbyte in bytearray(fh.read())]

This works because when you index a bytearray you just get back an integer (0-255), whereas if you just read a byte from the file you get back a single character string and so need to use ord to convert it to an integer.


If your file is too big to comfortably hold in memory (though I'm guessing it isn't) then an mmap could be used to create the bytearray from a buffer:

importmmapm= mmap.mmap(fh.fileno(), 0, access=mmap.ACCESS_READ)
ba = bytearray(m)

Solution 2:

You want to use ord instead of int:

if (ord(byte) & 0x01) == 0x01:

Solution 3:

One way:

import array

filebytes= array.array('B')
filebytes.fromfile(open("/tmp/test.txt", "rb"))
if all(i & 1for i in filebytes):
    # all file bytes are odd

Another way:

fobj= open("/tmp/test.txt", "rb")

try:
    import functools
except ImportError:
    bytereader= lambda: fobj.read(1)
else:
    bytereader= functools.partial(fobj.read, 1)

ifall(ord(byte) & 1for byte initer(bytereader, '')):
    # all bytes are odd
fobj.close()

Post a Comment for "Reading And Interpreting Data From A Binary File In Python"