Reputation: 37
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
Upvotes: 1
Views: 63
Reputation: 259
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.
Upvotes: 1