Search code examples
pythonlinuxnameerrortraceback

How do i deal with this traceback name error? This is my first script


The error is as follows

Traceback (most recent call last):
File "./unzipemall3.py", line 14, in <module>
   sformat = parentFormat('start')
File "./unzipemall3.py", line 9, in parentFormat
   parent = input(where + " Folder name (ex. Mol1-A) : ")
File "<string>", line 1, in <module>
NameError: name 'mol8' is not defined

!/usr/bin/env python

from os import chdir from subprocess import Popen, PIPE

def parentFormat(where): ## returns a list with the separator of '-' ## ex. Mol1-A returns [Mol1, A] # input for most parent folder parent = raw_input(where + " Folder name (ex. Mol1-A) : ") return parent.split('-')

if 'main' == name:

sformat = parentFormat('Start')
eformat = parentFormat('End')

cp_dir_prop = {'mol': cformat[0][:3], 'number': '5', 'letter': 'A'}
cp_files = ['vi.job', 'numjob', 'ortho.inp', 'job']
cp_dir = cp_dir_prop['mol'] + cp_dir_prop['number'] + "-" + cp_dir_prop['letter']
cp_dir = '/'.join([cp_dir, cp_dir]) + "-1"

try:
    for i in range(0, cutInt(eformat[0])):
        cformat[0] = sformat[0][:3] + str(cutInt(sformat[0]))

        for j in range(0, ord(eformat[1])-64):
            cformat[1] = chr(ord(sformat[1]) + j)

            # ex. mkdir Mol8-A/Mol8-A-1
            directory = '-'.join(cformat) + '/' + '-'.join(cformat + ['1'])
            Popen(['mkdir', directory])

            # ex. cp [files from Mol5-A-1] Mol8-A-1/
            for cp_file in cp_files:
                Popen(['cp', '/'.join([cp_dir, cp_file]), directory + '/'])

            # ex. tar -xf Mol8-A
            Popen(['tar', '-xf', '-'.join(cformat) + ".tgz"])

            # rm charmm-gui
            Popen(['rm', 'charmm-gui'])

            # ex. cd Mol8-A
            chdir('-'.join(cformat))

            # sed -i -e '0,/dimensions/s/dimensions/!dimensions.' -e '5s/.*/DIMENS CHSIZE 1000000/' -e '68s/.*/DYNA CPT leap start time 0.002 nstep 25000 -/' step5.1_production.inp
            sed = ['sed', '-i']
            sed.extend(['-e', '5s/.*/DIMENS CHSIZE 1000000/'])
            sed.extend(['-e', '68s/.*/DYNA CPT leap start time 0.002 nstep 25000 -/'])
            sed.extend(['-e', '0,/dimensions/s/dimensions/!dimensions'])
            sed.extend(['step5.1_production.inp'])
            Popen(sed)

            # sed -i -e '3s/nodes=4/nodes=1/' -e '37s:.*:mpirun ~/charmm.c36a4.20140107.newcali4.fixhcali.grange.b < ortho.inp   >charmm.out:' job5
            sed = ['sed', '-i']
            sed.extend(['-e', '3s/nodes=4/nodes=1/'])
            sed.extend(['-e', '37s:.*:mpirun ~/charmm.c36a4.20140107.newcali4.fixhcali.grange.b < ortho.inp   >charmm.out:'])
            sed.extend(['job5'])
            Popen(sed)

            # msub -q backfill job5
            Popen(['msub', '-q', 'backfill', 'job5'])

            # cd ..
            chdir('..')

except Exception as e:
    print (e)

Solution

  • I'm guessing you're using Python 2.7 or lower.

    parent = input(where + " Folder name (ex. Mol1-A) : ")
    

    On this line, if the user types mol8, the interpreter will look for the variable mol8 and try to assign it to parent. But this won't work because the variable mol8 doesn't exist. If you want parent to contain the string value "mol8", use raw_input.

    parent = raw_input(where + " Folder name (ex. Mol1-A) : ")
    

    Alternatively, upgrade to Python 3, where raw_input has been renamed to input.