1

I want to, as the title says, open a file with python in a binary mode I initially tried this with the 'rb' open method but this just returns data like this instead what I'm looking for is this:

01010101101001
Etch-a-sketch
  • 37
  • 1
  • 6

1 Answers1

1

You can use python built-in base64 library.

import base64

with open('/path/to/file','rb') as imageFile:
    str = base64.b64encode(imageFile.read())

imageBytes = base64.decodebytes(str)
imageBinary = "".join(["{:08b}".format(x) for x in imageBytes])

print(imageBinary)

will result in something like:

0011111110111011011110111011100000

Details can be seen on this answer.

Matthew Miles
  • 727
  • 12
  • 26