我正在使用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:DISTWR_4.5oHistory1.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:DISTWR_4.5oHistory1.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
我怎么可能清楚地声明一个全局变量,然后我收到此错误?
请帮助我。另外,如果需要完整的代码,请告诉我。
返回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:DISTWR_4.5oHistory1.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:DISTWR_4.5oHistory1.log', path_dir+name_list[0]+'-'+timestr+name_list[1])
print('Log átmásolva')
在你的主中,你可以像这样传递它:
...
path_dir = decider()
ask_user()
check_and_copy(path_dir)
...