How can I permanently store user inputs automatically into the dictionary

Question:

users = {
    "Hi":"HM123",
    "alan": "12122",
    "12": "11"
}
def adder():
    new_user = input("Please enter user's name: ").strip()
    new_pwd = ""
    confirmer = "0"
    while new_pwd != confirmer:
        new_pwd = input("please enter a new Password: ").strip()
        confirmer = input("please confirm your password: ").strip()
        if new_pwd != confirmer:
            print("passwords does not match!!")
    users[new_user] = new_pwd
adder()

I used The dictionary as a collection of usernames and passwords to practice creating a simple functional login page.(i’m importing this as a module to my main file). and when I add new users and passwords this code above temporarily adds it to the dictionary but when I re-run the script and try the new user names and pwds it returns incorect username and password, bc they are not in the dictionary.

was hoping to find a way to add the new usernames and paswwords into the dictionary permanently just with user inputs without having to modify the dictionary my self.

Asked By: Hamza mh

||

Answers:

Your dictionary (more or less) is stored in RAM which is voilatile – you cannot (or at least, you shouldn’t try to) preserve it between different scripts run.

Thas is why people use databases – they are stored on disk and don’t vanish unless something really bad happens 😉

The easiest what would suits your needs is to store them in a single json file. It is a format very similar to python dictionary. Python has json library that allows it to parse such file into pythons dict and the opposite – put the dict back into the file.

Here is the example:

import json

with open("users.json", "r+") as f:
    # convert content of file users.json into users variable - it will be a dict
    users = json.load(f)

def store_to_file(users):
    with open("users.json", "w") as f:
        # save the users dict into the file users.json in json format
        json.dump(users, f, indent=4)

def adder():
    ...
    store_to_file(users)

adder()

Do not forget to create the file users.json!

{
    "Hi": "HM123",
    "alan": "12122",
    "12": "11"
}
Answered By: kosciej16

Python dictionaries can be converted to JSON text and written to permanent storage as such.

You could also consider serialisation of the dictionary using the pickle module.

Here’s an example of both techniques:

import pickle
import json

PFILE = '/Volumes/G-Drive/users.pkl'
JFILE = '/Volumes/G-Drive/users.json'

users = {
    "Hi": "HM123",
    "alan": "12122",
    "12": "11"
}

with open(PFILE, 'wb') as db:
    pickle.dump(users, db) # save the dictionary (serialise)

with open(PFILE, 'rb') as db:
    _users = pickle.load(db) # retrieve serialised data
    print(_users)

with open(JFILE, 'w') as db:
    json.dump(users, db) # save as JSON

with open(JFILE) as db:
    _users = json.load(db) # retrieve JSON and convert to Python dictionary
    print(_users)

Output:

{'Hi': 'HM123', 'alan': '12122', '12': '11'}
{'Hi': 'HM123', 'alan': '12122', '12': '11'}
Answered By: Fred