稍后使用全局变量,python说它没有定义

时间:2018-02-02 07:32:46

标签: python global-variables selenium-chromedriver nameerror

我正在使用chromedriver进行一些自动化。而且我从我们公司的网络系统中获取了我的价值。

以下是我的代码的一部分:

def ask_user():
global choice
while True:
    try:
        choice = int(input('Milyen tesztet csinálsz? \n (1) Bejövő WR Teszt \n (2) Kimenő WR teszt \n'))
    except ValueError:
        print('Kérlek számot üss be!')
        continue
    if 0 < choice < 3:
        break
    else:
        print('Ez nem 1 és 2 között van! Próbáld újra:')

def decider():
    global path_dir
    if warr_type == warranty:
        with open('partners.json', encoding = 'utf-8') as partner_data:
            partner_db = json.load(partner_data)
    for p in partner_db.keys():
        if p == partner:
            path_dir = partner_db.get(p)           
    else:
        with open('data.json') as data_file:
            data = json.load(data_file)
    for k in data.keys():
        if k == restring.group(1):
            path_dir = data.get(k)


def check_and_copy(): # checking for directory existence and copy based upon user choice+ decider()
    if choice == 1:
        incoming()
    else:
        outgoing()
    if not os.path.exists(path_dir):
        os.makedirs(path_dir)
        name_list = os.path.splitext(name)
        shutil.copy2('C:\DIST\WR_4.5\oHistory1.log', path_dir+name_list[0]+'-'+timestr+name_list[1])
        print ('Log átmásolva')
    else:
        # os.chdir(target_dir) felesleges sor
        name_list = os.path.splitext(name)
        shutil.copy2('C:\DIST\WR_4.5\oHistory1.log', path_dir+name_list[0]+'-'+timestr+name_list[1])
        print('Log átmásolva')

我得到的错误是:

  File "C:/GSM/GSM_wr_AUTO_1.7.1(exp).py", line 120, in check_and_copy
if not os.path.exists(path_dir):
NameError: name 'path_dir' is not defined

我怎么可能清楚地声明一个全局变量,而且我收到了这个错误?

请帮帮我。如果需要完整的代码,请告诉我。

1 个答案:

答案 0 :(得分:1)

返回decider()

末尾的值
def decider():
    path_dir = ""
    if warr_type == warranty:
        with open('partners.json', encoding = 'utf-8') as partner_data:
            partner_db = json.load(partner_data)
        for p in partner_db.keys():
            if p == partner:
                path_dir = partner_db.get(p)          
    else:
        with open('data.json') as data_file:
            data = json.load(data_file)
        for k in data.keys():
            if k == restring.group(1):
                path_dir = data.get(k)
   return path_dir

然后在check_and_copy()传递该值

def check_and_copy(path_dir): # checking for directory existence and copy based upon user choice+ decider()
    if choice == 1:
        incoming()
    else:
        outgoing()
    if not os.path.exists(path_dir):
        os.makedirs(path_dir)
        name_list = os.path.splitext(name)
        shutil.copy2('C:\DIST\WR_4.5\oHistory1.log', path_dir+name_list[0]+'-'+timestr+name_list[1])
        print ('Log átmásolva')
    else:
        # os.chdir(target_dir) felesleges sor
        name_list = os.path.splitext(name)
        shutil.copy2('C:\DIST\WR_4.5\oHistory1.log', path_dir+name_list[0]+'-'+timestr+name_list[1])
        print('Log átmásolva')

在你的主要内容中你可以像这样传递它:

...
            path_dir = decider()
            ask_user()
            check_and_copy(path_dir)
...