In [4]:
with open('file.bin', 'wb') as f:
    f.write("1")

In [5]:
f.close


Out[5]:
<function close>

In [7]:
with open("file.bin", "rb") as binary_file:
    # Read the whole file at once
    data = binary_file.read()
    print(data)


1

In [8]:
one_byte = int('11110000', 2)
print(one_byte)


240

In [11]:
one_byte = int('000110001', 2)
print(one_byte)


49

In [18]:
a_byte = b'\x31'  # 255

In [13]:
i = ord(a_byte) # Get the integer value of the byte

In [14]:
bin = "{0:b}".format(i) # binary 1111111

In [15]:
print (bin)


11111111

In [19]:
with open('file.bin', 'wb') as f:
    f.write(a_byte)
f.close
# http://www.devdungeon.com/content/working-binary-data-python ; http://www.diveintopython3.net/serializing.html


Out[19]:
<function close>

In [23]:
with open("file.bin", "rb") as binary_file:
    # Read the whole file at once
    data = binary_file.read()
    print( data)


  File "<ipython-input-23-a71b942ddd05>", line 4
    print(%0bx data)
          ^
SyntaxError: invalid syntax

In [ ]: