Python Code Error

Lucky Tham
Lucky Tham used Ask the Experts™
on
Hi,

ps advice how to resolve the error. Tks

1.jpg
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
bbaoIT Consultant

Commented:
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.
Lucky ThamFreelancer

Author

Commented:
it is a row and not raw.  but it didn't output the error log. how to resolve it? tks
bbaoIT Consultant

Commented:
sorry for thr typo.

simply move the “row =“ line out of the “try” section, put it directly under “res =“ line.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Lucky ThamFreelancer

Author

Commented:
can you show me? tks
Lucky ThamFreelancer

Author

Commented:
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)))
bbaoIT Consultant

Commented:
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

Lucky ThamFreelancer

Author

Commented:
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
Lucky ThamFreelancer

Author

Commented:
still error. send you the whole code for yr easy reference. tks
main.txt
Lucky ThamFreelancer

Author

Commented:
Why it does not output the error files in JSON?
Lucky ThamFreelancer

Author

Commented:
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
Lucky ThamFreelancer

Author

Commented:
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.")

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial