Python Forum

Full Version: reading the binary contents of any file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was wondering how to make it so I can load the binary of any file
so say I have a file called,
file1.qra
(qra is made up)
and the binary contents of it are:
011000010110001001100011
I wanna be able to load the binary
contents in any file to python,
and with file1.qra
it would return a string:
011000010110001001100011

so, how do I do this?
just open the file as mode 'rb'
so i have the file:
file1.txt
and its contents are:
abc
this is what i did
with open(file1.txt, 'rb') as f: # Open data input file
     f_contents = f.read()
     print(f_contents)
and it gave me this:
b'abc'
when I printed f_contents
Quote:load the binary of any file
I took above to mean you wanted to open the file in binary mode.
I think what you're saying is that you want to display the contents of a text file as binary.
Coorrect?
(Sep-15-2018, 07:18 PM)qrani Wrote: [ -> ]I was wondering how to make it so I can load the binary of any file
so say I have a file called,
file1.qra
(qra is made up)
and the binary contents of it are:
011000010110001001100011
I wanna be able to load the binary
contents in any file to python,
and with file1.qra
it would return a string:
011000010110001001100011

so, how do I do this?

Read a file content in a binary mode - and the convert every byte with a secret Snooty (just kidding) bin function