How to Read Bytes from a file

gregpinero at gmail.com gregpinero at gmail.com
Thu Mar 1 01:52:56 EST 2007


It seems like this would be easy but I'm drawing a blank.

What I want to do is be able to open any file in binary mode, and read
in one byte (8 bits) at a time and then count the number of 1 bits in
that byte.

I got as far as this but it is giving me strings and I'm not sure how
to accurately get to the byte/bit level.

f1=file('somefile','rb')
while 1:
    abyte=f1.read(1)

Thanks in advance for any help.

-Greg




More information about the Python-list mailing list