Link to home
Start Free TrialLog in
Avatar of Lucky Tham
Lucky Tham

asked on

Python Code Error

Hi,

ps advice how to resolve the error. Tks

User generated image
Avatar of bbao
bbao
Flag of Australia image

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.
Avatar of Lucky Tham
Lucky Tham

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.
can you show me? tks
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(volume_errors)))
print("Got {1} error(s) in creating vms".format(len(vm_errors)))
I mean to change below code FROM

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)

Open in new window


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)

Open in new window

InsecureRequestWarning)
Traceback (most recent call last):
  File "C:/Users/Owner/PycharmProjects/fusion/main.py", line 71, in <module>
    row = {"VM_id": res['server']['id'], "volume_id": volume_id}
KeyError: 'server'

Process finished with exit code 1
still error. send you the whole code for yr easy reference. tks
main.txt
Why it does not output the error files in JSON?
File "C:/Users/P1319181/PycharmProjects/fusion/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
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(volume_errors)))
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 TRIAL
Members 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.