Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read binary from file
#1
I have some trouble reading binary file



with open('myfile', 'rb') as fl:
    b = fl.read( 1 )
    for x in range( 0,4):
         tuppleCounter[ ( ( 3 << ( x * 2 ) ) & b ) >> ( x*2 ) ]+=1
The interpreter says
TypeError: unsupported operand type(s) for &: 'int' and 'str'

Also
b = int( fl.read( 1 ), 10 )
and
b = int( fl.read( 1 ) )
for both says
ValueError: invalid literal for int() with base 10: 'i'

i dont know what im doing wrong
Reply
#2
Please post entire error traceback verbatim
supple sample file if possible, or where it can be downloaded
b will read 1 byte, starting at byte number 1 in file
You don't show tuppleCounter
is this what you wanted?

You don't need to supply number base to int if decimal
If I try with random binary file:
>>> with open('image_002.jpg', 'rb') as fp:
...     b = fp.read(1)
... 
>>> b
b'\xff'
>>> for x in range( 0,4):
...     tuppleCounter[ ( ( 3 << ( x * 2 ) ) & b ) >> ( x*2 ) ]+=1
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'tuppleCounter' is not defined
Reply
#3
if __name__ == "__main__":

    tuppleCounter = [ 0, 0, 0 ,0 ]

    with open('myfile', 'rb') as fl:
        b = fl.read( 1 )

        for x in range( 0,4):
            tuppleCounter[ ( ( 3 << ( x * 2 ) ) & b ) >> ( x*2 ) ]+=1

    print tuppleCounter;

nextor@leon:~/Projects/tmp/pypress$ python test.py
Traceback (most recent call last):
File "test.py", line 10, in <module>
tuppleCounter[ ( ( 3 << ( x * 2 ) ) & b ) >> ( x*2 ) ]+=1
TypeError: unsupported operand type(s) for &: 'int' and 'str'
nextor@leon:~/Projects/tmp/pypress$
Reply
#4
Are you using PYTHON 2 ?????
Reply
#5
(Nov-27-2018, 08:45 PM)Gribouillis Wrote: Are you using PYTHON 2 ?????
it says Python 2.7.15+
And with Python 3.6.7rc1 it says the same
Reply
#6
Python 2 is almost dead (still supported during one year), you should be using python 3.5 or 3.6 or 3.7.
You need to convert the char you read to an int. In python 2, do
b = fl.read(1)
b = ord(b)
In python 3, use
b = fl.read(1)
b = b[0]
Reply
#7
It works, Thanks Gribouillis
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,049 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Read TXT file in Pandas and save to Parquet zinho 2 1,244 Sep-15-2024, 06:14 PM
Last Post: zinho
  Pycharm can't read file Genericgamemaker 5 1,574 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 3,674 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,288 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 4,696 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,820 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 2,591 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 2,240 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 4,991 Jun-19-2023, 02:12 PM
Last Post: DosAtPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020