Json file and dump function

def registration():
    Full_name=input("Name:")
    Phone_num=input("Phone number:")
    Email=input("Email:")
    Address=input("Address:")
    Password=input("Password:")
    with open('Customer.json','w') as f:
        cust[Email]={"Name":Full_name,"Contact_Number":Phone_num,"Email":Email,"Address":Address,"Password":Password,"Order history":None}
        json.dump(cust,f)

if __name__=="__main__":
    print('_'*30,'Welcome','_'*30)
    cust={} 
    registration()

The json file is getting over written whenever I call the function.
I want my json file in {1:{…},2:{…}} this format.
Please rectify the mistake and help me correcting this code.

2 Likes

@cmsathwik44
Do not define the variable inside json.dump, update the dictionary outside and dump it again in json using write mode ( β€˜w’)

1 Like

It’s present outside the dump function right sir/mam?
and I have created the dictionary in main function.

1 Like

No it is not present outside. You are updating your dictionary after opening your json file.
Try to update before hand

    cust[Email]={"Name":Full_name,"Contact_Number":Phone_num,"Email":Email,"Address":Address,"Password":Password,"Order history":None}
    with open('Customer.json','w') as f:
        json.dump(cust,f)

Sir I tried this also but still the json file is getting overwritten.
please help me.

@cmsathwik44
Please try append mode
Or
You can try to seek your pointer to last point.

Try this once