title: Convert file to binary code in Python
body: <p>I am looking to convert a file to binary for a project, preferably using Python as I am most comfortable with it, though if walked-through, I could probably use another language.</p>
  
  <p>Basically, I need this for a project I am working on where we want to store data using a DNA strand and thus need to store files in binary ('<strong><code>A</code></strong>'s and '<strong><code>T</code></strong>'s <code>= 0</code>, '<strong><code>G</code></strong>'s and '<strong><code>C</code></strong>'s <code>= 1</code>)</p>
  
  <p>Any idea how I could proceed? I did find that use could encode in <strong><code>base64</code></strong>, then decode it, but it seems a bit inefficient, and the code that I have doesn't seem to work...</p>
  
  <pre><code>import base64
  import tkinter as tk
  from tkinter import filedialog
  
  root = tk.Tk()
  root.withdraw()
  file_path = filedialog.askopenfilename()
  print(file_path)
  with open(file_path) as f:
      encoded = base64.b64encode(f.readlines())
      print(encoded)
  </code></pre>
  
  <p>Also, I already have a program to do that simply with text. Any tips on how to improve it would also be appreciated! </p>
  
  <pre><code>import binascii
  t = bytearray(str(input("Texte?")), 'utf8')
  h = binascii.hexlify(t)
  b = bin(int(h, 16)).replace('b','') 
  #removing the b that appears in the end for some reason
  g = b.replace('1','G').replace('0','A')
  print(g)
  </code></pre>
  
  <p>For example, if I input test:
  ok so for the text to DNA: 
  I input 'test' and expect the DNA sequence that comes from the binary
  the binary being: 01110100011001010111001101110100 (Also I asked to print every conversion in the example so that it is more comprehensible) </p>
  
  <pre><code>&gt;&gt;&gt;Texte?test #Asks the text
  &gt;&gt;&gt;b'74657374' #converts to hex
  &gt;&gt;&gt;01110100011001010111001101110100 #converts to binary
  &gt;&gt;&gt;AGGGAGAAAGGAAGAGAGGGAAGGAGGGAGAA #converts 0 to A and 1 to G
  </code></pre>