我如何处理此追溯名称错误?这是我的第一个脚本

时间:2014-07-21 18:35:57

标签: python linux nameerror traceback

错误如下

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

来自os import chdir的

来自子进程导入Popen,PIPE

def parentFormat(where):     ##返回一个列表,其中包含&#39; - &#39;     ## ex。 Mol1-A返回[Mol1,A]     #输入大多数父文件夹     parent = raw_input(其中+&#34;文件夹名称(例如Mol1-A):&#34;)     返回parent.split(&#39; - &#39;)

如果&#39; 主要&#39; == 名称

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)

1 个答案:

答案 0 :(得分:3)

我猜你正在使用Python 2.7或更低版​​本。

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

在此行中,如果用户键入mol8the interpreter will look for the variable mol8并尝试将其分配给parent。但这不起作用,因为变量mol8不存在。如果您希望parent包含字符串值"mol8",请使用raw_input

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

或者,升级到Python 3,其中raw_input已重命名为input