Lucky Tham
asked on
Python Code Error
the variable “row” is not defined in the “Except” section, though it is defined in “Try” section (which is not visible for the “Except” section.
ASKER
it is a row and not raw. but it didn't output the error log. how to resolve it? tks
sorry for thr typo.
simply move the “row =“ line out of the “try” section, put it directly under “res =“ line.
simply move the “row =“ line out of the “try” section, put it directly under “res =“ line.
ASKER
can you show me? tks
ASKER
try:
row = {"VM_id": res['server']['id'], "volume_id": volume_id}
vms.append(row)
except:
vm_errors.append(row)
with open(output_file, 'w+') as output_json:
json.dump(vms, output_json)
error_data = {'volumes':volume_errors, 'vms':vm_errors}
with open(output_error_file, 'w+') as error_file:
json.dump(error_file, error_data)
print("Total {0} VMs and volumes created.".format(len(vms)) )
print("Got {0} error(s) in creating system volumes.".format(len(volum e_errors)) )
print("Got {1} error(s) in creating vms".format(len(vm_errors) ))
row = {"VM_id": res['server']['id'], "volume_id": volume_id}
vms.append(row)
except:
vm_errors.append(row)
with open(output_file, 'w+') as output_json:
json.dump(vms, output_json)
error_data = {'volumes':volume_errors, 'vms':vm_errors}
with open(output_error_file, 'w+') as error_file:
json.dump(error_file, error_data)
print("Total {0} VMs and volumes created.".format(len(vms))
print("Got {0} error(s) in creating system volumes.".format(len(volum
print("Got {1} error(s) in creating vms".format(len(vm_errors)
I mean to change below code FROM
TO
try: row = {"VM_id": res['server']['id'], "volume_id": volume_id} vms.append(row) except: vm_errors.append(row) with open(output_file, 'w+') as output_json: json.dump(vms, output_json)
TO
row = {"VM_id": res['server']['id'], "volume_id": volume_id}
try:
vms.append(row)
except:
vm_errors.append(row)
with open(output_file, 'w+') as output_json:
json.dump(vms, output_json)
ASKER
InsecureRequestWarning)
Traceback (most recent call last):
File "C:/Users/Owner/PycharmPro jects/fusi on/main.py ", line 71, in <module>
row = {"VM_id": res['server']['id'], "volume_id": volume_id}
KeyError: 'server'
Process finished with exit code 1
Traceback (most recent call last):
File "C:/Users/Owner/PycharmPro
row = {"VM_id": res['server']['id'], "volume_id": volume_id}
KeyError: 'server'
Process finished with exit code 1
ASKER
still error. send you the whole code for yr easy reference. tks
main.txt
main.txt
ASKER
Why it does not output the error files in JSON?
ASKER
File "C:/Users/P1319181/Pycharm Projects/f usion/main _v1.py", line 58, in <module>
res = fusion.create_vm(vm_name, flavorRef, volume_id, network_id, key_name, access_token)
TypeError: create_vm() takes 5 positional arguments but 6 were given
Process finished with exit code 1
res = fusion.create_vm(vm_name, flavorRef, volume_id, network_id, key_name, access_token)
TypeError: create_vm() takes 5 positional arguments but 6 were given
Process finished with exit code 1
ASKER
error code from line 58:
res = fusion.create_vm(vm_name, flavorRef, volume_id, network_id, key_name, access_token)
try:
row = {"VM_id": res['server']['id'], "volume_id": volume_id}
vms.append(row)
except:
vm_errors.append(res)
with open(output_file, 'w+') as output_json:
json.dump(vms, output_json)
error_data = {'volumes':volume_errors, 'vms':vm_errors}
with open(output_error_file, 'w+') as error_file:
json.dump(error_file, error_data)
print("Total {0} VMs and volumes created.".format(len(vms)) )
print("Got {0} error(s) in creating system volumes.".format(len(volum e_errors)) )
print("Got {0} error(s) in creating vms".format(len(vm_errors) ))
print("Check the outputs folder for more information.")
res = fusion.create_vm(vm_name, flavorRef, volume_id, network_id, key_name, access_token)
try:
row = {"VM_id": res['server']['id'], "volume_id": volume_id}
vms.append(row)
except:
vm_errors.append(res)
with open(output_file, 'w+') as output_json:
json.dump(vms, output_json)
error_data = {'volumes':volume_errors, 'vms':vm_errors}
with open(output_error_file, 'w+') as error_file:
json.dump(error_file, error_data)
print("Total {0} VMs and volumes created.".format(len(vms))
print("Got {0} error(s) in creating system volumes.".format(len(volum
print("Got {0} error(s) in creating vms".format(len(vm_errors)
print("Check the outputs folder for more information.")
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.