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())
for byte in ba:
print byte & 1
or to create a list of results:
low_bit_list = [byte & 1 for byte 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:
import mmap
m = mmap.mmap(fh.fileno(), 0, access=mmap.ACCESS_READ)
ba = bytearray(m)
Answer from Scott Griffiths on Stack OverflowTry 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())
for byte in ba:
print byte & 1
or to create a list of results:
low_bit_list = [byte & 1 for byte 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:
import mmap
m = mmap.mmap(fh.fileno(), 0, access=mmap.ACCESS_READ)
ba = bytearray(m)
You want to use ord instead of int:
if (ord(byte) & 0x01) == 0x01:
Python workflow for reading binary file into byte array
Whats a good way of writing an -int- as BYTES into a binary file (and later, reading it in too) ?
python - Reading binary file and looping over each byte - Stack Overflow
How to Read Binary File in Python?
Lets say I have two ints 35400, 364. I need to write these into a binary file one after the other.
How do you do write that int , and how do you open that file and read those ints in later?
(Using Path. if possible)
Python >= 3.8
Thanks to the walrus operator (:=) the solution is quite short. We read bytes objects from the file and assign them to the variable byte
with open("myfile", "rb") as f:
while (byte := f.read(1)):
# Do stuff with byte.
Python >= 3
In older Python 3 versions, we get have to use a slightly more verbose way:
with open("myfile", "rb") as f:
byte = f.read(1)
while byte != b"":
# Do stuff with byte.
byte = f.read(1)
Or as benhoyt says, skip the not equal and take advantage of the fact that b"" evaluates to false. This makes the code compatible between 2.6 and 3.x without any changes. It would also save you from changing the condition if you go from byte mode to text or the reverse.
with open("myfile", "rb") as f:
byte = f.read(1)
while byte:
# Do stuff with byte.
byte = f.read(1)
Python >= 2.5
In Python 2, it's a bit different. Here we don't get bytes objects, but raw characters:
with open("myfile", "rb") as f:
byte = f.read(1)
while byte != "":
# Do stuff with byte.
byte = f.read(1)
Note that the with statement is not available in versions of Python below 2.5. To use it in v 2.5 you'll need to import it:
from __future__ import with_statement
In 2.6 this is not needed.
Python 2.4 and Earlier
f = open("myfile", "rb")
try:
byte = f.read(1)
while byte != "":
# Do stuff with byte.
byte = f.read(1)
finally:
f.close()
This generator yields bytes from a file, reading the file in chunks:
def bytes_from_file(filename, chunksize=8192):
with open(filename, "rb") as f:
while True:
chunk = f.read(chunksize)
if chunk:
for b in chunk:
yield b
else:
break
# example:
for b in bytes_from_file('filename'):
do_stuff_with(b)
See the Python documentation for information on iterators and generators.