Accessing pickled data?

So I’m super new to Python, and I thought a good first program would be a sort of login project. Essentially it just takes the user input and puts it as key:value in a dictionary. I’ve run into a couple main issues.

  1. When the program is run and the user is asked to log in or sign up, if the user makes an account they can then login to that account after. But the second they exit the program and try to login to the account again, the account doesn’t exist. If I put the Key:Value directly into the dictionary it can then be used as an account and is saved.

  2. I want to be able to save the account in a separate file so that it isn’t completely visible everytime the IDLE is opened. I am trying to pickle it, and that seems to work.
    For instance, if the username is “John” and the password is “Doe” this is what is saved in the
    file: ��}��John��Doe�s.

My main question is how can I now make it so that if the program is run again, the user can type “John” and “Doe” and successfully login?

Thanks.

import pickle


users = {}
status = ""

def mainDisplay():
    status = input("Do you have an account?: y/n? Press ! to quit: ")
    if status == "y":
        existUser()
    elif status == "n":
        newUser()
    elif status == "!":
        quit()

def newUser():
    createAccount = input("Please create a username: ")

    if createAccount in users:
        print("\nUsername already taken.\n")
    else:
        createPass = input("Create a password")
        users[createAccount] = createPass
        print("\nUser created\n")
        pickle.dump(users, open("passwords.txt", "wb"))
def existUser():
    login = input("Enter your username: ")
    passw = input("Enter your password: ")

    if login in users and users[login] == passw:
        print("\nLogin successful.\n")
    else:
        print("\nError: invalid username or password and/or invalid combination.")



while status != "!":
    mainDisplay()



Welcome, Fel.py.

From what I have read, most of your questions’ answers are on the Pickle Documentation.

You are just missing the part where you “unpickle” the data:

with open("passwords.txt","rb") as passes:
   pickle.load(passes)

Iterate through that to get your correct data.

Otherwise, have a look through the documentation on avoiding globals.

Hope this helps.

I got it! I feel kind of stupid now because the answer was just a simple pickle command. I just had to type the code below right underneath “def existUser” thanks for the reply, the link you provided was great.

users = pickle.load(open("passwords.txt", "rb"))