-1

What is the easiest way to get the underlying binary code(0s and 1s) for a given file? The context for this question is that I want a python function which takes a file name, looks it up and gathers the binary code for that file before either storing it somewhere or returning it. After this I want to do some manipulations on the binary file.

Mark Keane
  • 984
  • 2
  • 11
  • 26
  • 1
    There are no underlying 0s and 1s for a given file, it's an idea born of confusion. If you want to write a program which represents a single bit of binary data as *8-bit ASCII characters one and zero*, you certainly can do it. But it's not a case of "*viewing the underlying data as it really is*", it's "*calculating one possible represenation*". And if you're doing that ... "everyone" does that in Hex because it's a more direct, concise mapping. As argued here: http://stackoverflow.com/questions/37103607/ and then it is a duplicate, e.g. http://stackoverflow.com/a/2894216/478656 – TessellatingHeckler Jul 04 '16 at 21:24

1 Answers1

1

The underlying code for a file is available form the .read() method of the file object. Use the b mode modifier when you open the file:

with open("input_file.bin", "rb") as input_file:
    bits = input_file.read() 

If you want to easily manipulate the bits after reading them in, you might want to convert them to a bitarray:

from bitarray import bitarray
with open("input_file.bin", "rb") as input_file:
    chars = input_file.read()

bits = bitarray()
bits.frombytes(chars)
print bits.count(1), bits.count(0)

References:

Robᵩ
  • 163,533
  • 20
  • 239
  • 308