NameError:没有定义全局名称'b'。

时间:2021-04-07 20:26:28

I have an script wih the following structure:

我有一个脚本wih如下结构:

def func():
    bfile=open(b, 'r')
    cfile=open(c, 'r')
    dfile=open(d, 'r')

if __name__=='__main__':
    if len(sys.argv)==1:
        print 'guidence'
        sys.exit()
    else:
        opts,args=getopt.getopt(sys.argv,'b:c:d:a')
        a=False
        for opt,arg in opts:
            if opt=='-a':
                a=True
            elif opt=='-b':
                b=arg
            elif opt=='-c':
                c=arg
            elif opt=='-d':
                d=arg
        func()

I run it in this way:

我以这种方式运行:

# python script.py -b file1 -c file1 -d file1

in func()
NameError: global name 'b' is not defined

I also define a global file1. but no work.

我还定义了全局文件1。但没有工作。

Update

I know where the problem is: the result of print opts is []. It has not values. why?

我知道问题在哪里:打印选项的结果是[]。它没有价值。为什么?

2 个解决方案

#1


0  

You have two issues.

你有两个问题。

First, getopt stops if it encounters a non-option. Since sys.argv[0] is the script name, it's a non-option and the parsing stops there.
You want to pass sys.argv[1:] to getopt.

首先,getopt在遇到非选项时停止。因为系统。argv[0]是脚本名称,它是一个非选项,解析在那里停止。你想通过sys。getopt argv[1:]。

This is what's causing print opts to show [], and since opts is empty the global variables are never created, which is where the complaint that they don't exist come from.

这是导致print opts显示的原因,因为opts是空的,所以全局变量不会被创建,这就是他们不存在的抱怨的来源。

Second, since -a wants a parameter, you need a colon after it.

其次,因为a需要一个参数,所以需要一个冒号。

In all, this should work:

总之,这应该奏效:

opts, args = getopt.getopt(sys.argv[1:], 'b:c:d:a:')

Also, you shouldn't pass parameters in globals.

此外,您不应该在全局变量中传递参数。

#2


0  

You have to pass the filenames to the function. In Your case it should look like this:

必须将文件名传递给函数。在你的情况下,它应该是这样的:

def func(b, c, d):
  bfile = open(b, 'r')
  cfile = open(c, 'r')
  dfile = open(d, 'r')

You can call it with:

你可以这样称呼它:

b = 'nameB.txt'
c = 'nameC.txt'
d = 'nameD.txt'
func(b, c, d)

#1


0  

You have two issues.

你有两个问题。

First, getopt stops if it encounters a non-option. Since sys.argv[0] is the script name, it's a non-option and the parsing stops there.
You want to pass sys.argv[1:] to getopt.

首先,getopt在遇到非选项时停止。因为系统。argv[0]是脚本名称,它是一个非选项,解析在那里停止。你想通过sys。getopt argv[1:]。

This is what's causing print opts to show [], and since opts is empty the global variables are never created, which is where the complaint that they don't exist come from.

这是导致print opts显示的原因,因为opts是空的,所以全局变量不会被创建,这就是他们不存在的抱怨的来源。

Second, since -a wants a parameter, you need a colon after it.

其次,因为a需要一个参数,所以需要一个冒号。

In all, this should work:

总之,这应该奏效:

opts, args = getopt.getopt(sys.argv[1:], 'b:c:d:a:')

Also, you shouldn't pass parameters in globals.

此外,您不应该在全局变量中传递参数。

#2


0  

You have to pass the filenames to the function. In Your case it should look like this:

必须将文件名传递给函数。在你的情况下,它应该是这样的:

def func(b, c, d):
  bfile = open(b, 'r')
  cfile = open(c, 'r')
  dfile = open(d, 'r')

You can call it with:

你可以这样称呼它:

b = 'nameB.txt'
c = 'nameC.txt'
d = 'nameD.txt'
func(b, c, d)