0

I want to be able to get a file(not just text files, I mean video files, word files, exe files etc...) and read its data in python. Then , I want to convert it to pure binary (1s and 0s) and then be able to decode that too.

I have tried just reading the file with

with open('a.mp4', 'rb') as f:
    ab = f.read()

and it seemed to work, but my function of

def toBinary(a):
  l,m=[],[]
  for i in a:
    l.append(ord(i))
  for i in l:
    m.append(int(bin(i)[2:]))
  return m

does not work on it, because I think ord() only works on text??? Not really sure

4
  • What exactly do you want? Print the file content in binary format like "10011011 00101000"? Commented Apr 9, 2023 at 9:55
  • Yes, I want to do that Commented Apr 9, 2023 at 10:10
  • You neither need ord nor int for this. Therefore you also don't need the list l. Commented Apr 9, 2023 at 10:17
  • Does this answer your question? How can I convert bytes object to decimal or binary representation in Python? Commented Apr 9, 2023 at 10:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.