How to fix NameError? I am working on a project and am having issues with the following code that I have written in nano:
from Bio import SeqIO
import sys
import re 


    fasta_file = (sys.argv[1])
    for myfile in SeqIO.parse(fasta_file, &quot;fasta&quot;):
      if len(myfile) &gt; 250:
       gene_id = myfile.id
       mylist = re.match('H149xcV\_\w+\_\w+\_\w+',gene_id)
       print (&quot;&gt;&quot;+list.group(1)) 

This is the error I receive when I execute my command on command-line:
File &quot;mpo.py&quot;, line 7, in &lt;module&gt;
    gene_id = myfile.id
NameError: name 'myfile' is not defined

I have a fasta file with the format
&gt;H149xcV_Fge342_r3_h2_d1 len=210 path=[0:0-206]
ACTATACATGAGGAGAACATAGAACAAAAATGGGACCATAGATATATAACAATAGAAGATATAGAGAACACAATAGACAACTTATTAGGAAAGAGGTGTGTCGTCATGGAGCTGATGTTCGAGGATACTTTGCATGGTCATTCTTGGATAATTTTGAGTGGGCTATGGGATACACCAAGAGGTTTGGCATTGTTTATGTTGATTATAAGAACGGGC

&gt;H149xcV_ytR1oP_r3_h2_d1 len=306 path=[0:0-207]
ATTAGAGTCTGAGAGAGTCTTGATTTGTCGTCGTCGAGAAATATAGGAGATCTGATTAGAGGAGAGAGCGGCCTAGGCGATGCGCGATATAGCGCTATATAGGCCTAGAGGAGAGTCTCTCTCTTTTAGAAGAGATAATATATATATATATATGGCTCTCCGGCGGGGCCGCGCGAGAGCTCGATCGATCGATATTAGCTGTACGATGCTAGCTAGCTTATATTCGATCGATTATAGCTTAGATCTCTCTCTAAAGGTCGATATCGCTTATGCGCGCGTATATCG

I wish to reformat my file so that it only provides me with the unique gene id's for genes with a length greater than 250 bp.
My desired output is:
&gt;H149xcV_Fge342_r3_h2
&gt;H149xcV_ytR1oP_r3_h2
&gt;H149xcV_DPN78333_r3_h2
&gt;H149xcV_AgV472_r3_h2
&gt;H149xcV_DNP733_r3_h2
You wrote,
mylist = re.match(H149xcV\_\w+\_\w+\_\w+)

try...
myregex = re.compile(r'H149xcV\_\w+\_\w+\_\w+')
mylist = myregex.match(myfile.id)

or
mylist = re.match(r'H149xcV\_\w+\_\w+\_\w+',myfile.id)

Watch your indentation it should be 4 spaces. Note you are losing the last '_\w+', but don't think it matters.
I think it is better to use,
mylist, _, _ = re.split(r' ', myfile.id)

The problem with your code is list will just return true ... and that is not what you want. If you are using split carefully watch the number of ' ' in your code you can also use r'\s'
You can also set maxsplit as a parameter, which might be useful.

myfile not defined.
Please try,
fasta_file = (sys.argv[0])

Generally your argv command isn't cool and not just [0] versus [1]. I forget the argv module everyone uses .. there's two of them, they are a bit fiddle but are very cool of options. You way in is okay, okayish, sort of okay ... Anyway you appear to be requesting the second input into the commandline, wherea you are probably placing the fasta file in the first position.
Also
print (fasta_file)

Check something happens that you picked up the file.
The other way in is,
mypath = '/User/username/location/fasta.fa'
for myfile in SeqIO.parse(mypath, &quot;fasta&quot;):

where mypath is a OS X style, you're probably using Linux so its... /home/username/pathtofile/fastafile.fa (something like that) or
for myfile in SeqIO.parse('/User/username/location/fasta.fa', &quot;fasta&quot;):

I forgot .... don't ever use list as a variable, by mylist is fine. Again I've not debugged my code.