title: Opening and editing multiple files in a folder with python
body: <p>I am trying to modify my .fasta files from this:</p>
  
  <pre><code>&gt;YP_009208724.1 hypothetical protein ADP65_00072 [Achromobacter phage phiAxp-3]
  MSNVLLKQ...
  
  &gt;YP_009220341.1 terminase large subunit [Achromobacter phage phiAxp-1]
  MRTPSKSE...
  
  &gt;YP_009226430.1 DNA packaging protein [Achromobacter phage phiAxp-2]
  MMNSDAVI...
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>&gt;Achromobacter phage phiAxp-3
  MSNVLLKQ...
  
  &gt;Achromobacter phage phiAxp-1
  MRTPSKSE...
  
  &gt;Achromobacter phage phiAxp-2
  MMNSDAVI...
  </code></pre>
  
  <p>Now, I've already have a script that can do it to a single file:</p>
  
  <pre><code>with open('Achromobacter.fasta', 'r') as fasta_file:
      out_file = open('./fastas3/Achromobacter.fasta', 'w')
      for line in fasta_file:
          line = line.rstrip()
          if '[' in line:
              line = line.split('[')[-1]
              out_file.write('&gt;' + line[:-1] + "\n")
          else:
              out_file.write(str(line) + "\n")
  </code></pre>
  
  <p>but I can't get to automate the process for all 120 files in my folder.</p>
  
  <p>I tried using glob.glob, but I can't seem to make it work:</p>
  
  <pre><code>import glob
  
  for fasta_file in glob.glob('*.fasta'):
      outfile = open('./fastas3/'+fasta_file, 'w')
      with open(fasta_file, 'r'):
          for line in fasta_file:
              line = line.rstrip()
              if '[' in line:
                  line2 = line.split('[')[-1]
                  outfile.write('&gt;' + line2[:-1] + "\n")
              else:
                  outfile.write(str(line) + "\n")
  </code></pre>
  
  <p>it gives me this output:</p>
  
  <pre><code>A
  c
  i
  n
  e
  t
  o
  b
  a
  c
  t
  e
  r
  .
  f
  a
  s
  t
  a
  </code></pre>
  
  <p>I managed to get a list of all files in the folder, but can't open certain files using the object on the list.</p>
  
  <pre><code>import os
  
  
  file_list = []
  for file in os.listdir("./fastas2/"):
      if file.endswith(".fasta"):
          file_list.append(file)
  </code></pre>