Python Code Error

Hi,

ps advice how to resolve the error. Tks

1.jpg
Lucky ThamFreelancerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bbaoIT ConsultantCommented:
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 ThamFreelancerAuthor Commented:
it is a row and not raw.  but it didn't output the error log. how to resolve it? tks
bbaoIT ConsultantCommented:
sorry for thr typo.

simply move the “row =“ line out of the “try” section, put it directly under “res =“ line.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Lucky ThamFreelancerAuthor Commented:
can you show me? tks
Lucky ThamFreelancerAuthor 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 ConsultantCommented:
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 ThamFreelancerAuthor 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 ThamFreelancerAuthor Commented:
still error. send you the whole code for yr easy reference. tks
main.txt
Lucky ThamFreelancerAuthor Commented:
Why it does not output the error files in JSON?
Lucky ThamFreelancerAuthor 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 ThamFreelancerAuthor 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.")
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Cloud Computing

From novice to tech pro — start learning today.