title: Splitting FASTA sequence using Python
body: <p>The following error message appears when I try to run the following script: 
  Background: I am trying to split up a large FASTA file (~45Mb) into smaller files based on gene id. I would like to chop it everytime the ">" appears. The following .py script allows me to do so. However, everynow and then I get the following error. Any feedback would be greatly appreciated. </p>
  
  <pre><code>Script:
   import os 
      os.chdir("/vmb/Flavia_All/Python_Commands")
      outfile = os.chdir("/vmb/Flavia_All/Python_Commands")
  
  import sys
  infile = open(sys.argv[1])
  outfile = []
  
  for line in infile:
      if line.startswith("&gt;"):
          if (outfile != []): outfile.close()
          genename = line.strip().split('|')[1]
          filename = genename+".fasta"
          outfile = open(filename,'w')
          outfile.write(line)
      else:
          outfile.write(line)
  outfile.close()
  </code></pre>
  
  <p>Error Message when script is run: </p>
  
  <pre><code>Traceback (most recent call last):
    File "splitting_fasta.py", line 14, in &lt;module&gt;
      outfile = open(filename,'w')
  IOError: [Errno 2] No such file or directory: 'AY378100.1_cds_AAR07818.1_173 [gene=pbrB/pbrC] [protein=PbrB/PbrC] [protein_id=AAR07818.1] [location=complement(152303..153451)].fasta'
  </code></pre>
  
  <p>*NOTE: AY378100.1_cds_AAR07818.1 is one of many genes in this FASTA sequence. This is not the only gene i have had the same message appear with. I would like to stop deleting each gene that comes up with this message.</p>