Reputation: 509
I'm doing some automatization with chromedriver. And i'm getting my values from our company's web based system.
Here is a portion of my code:
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')
And the error i'm getting is:
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
How is that possible that i clearly declare a global variable, and i'm getting this error?
Please help me. Also if the full code is needed let me know.
Upvotes: 0
Views: 53
Reputation: 1037
Return the value at the end of 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
Then in check_and_copy()
pass on that value
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')
In your main you can then pass it like so :
...
path_dir = decider()
ask_user()
check_and_copy(path_dir)
...
Upvotes: 1