How can I check if a dictionary (in an external file) contains a user entered username (Python3)?

I am creating a Flea Market program. I have an external file that stores all the usernames and passwords of the employees. I am trying to test the login section by asking for a username and then a password. It checks if the UN is in the dictionary contained in readline ().

Here is an external file with usernames and passwords.

managers = {"manager":"password", "owner":"apple"}
employees = {"jane":"none", "john":"banana"}


And here is the code .:

print("Welcome to Flea Master 2000...\n")
read_employee_file = open('employees_list.txt', 'r')
managers = read_employee_file.readline(0)
employees = read_employee_file.readline(1)

user_id = input("User ID:\n")
user_password = input('Password:\n')
if user_id in managers[:]:
    if managers[user_id] == user_password:
        print("Welcome, {0}.".format (user_id))
if user_id in employees:
    if employees[user_id] == user_password:
        print("Welcome, {0}".format (user_id))
        user_status = 'staff'
if user_status == 'manager':
    action_manager = int(input("Options: (Input number to select...)\n1) Add employee.\n2) Remove employee.\n"))
    if action_manager == 1:
        employee_addition_type=input("What kind of employee is he/she? ('manager' or 'staff')")
        if employee_addition_type == 'manager':
            new_manager_username = input("Enter the new manager username...\n")
            new_manager_password = input("Enter the new manager password...\n")
            managers[new_manager_username] = new_manager_password
            new_staff_username = input("Enter the new staff member username...\n")
            new_staff_password = input("Enter the new staff member password...\n")

    if action_manager == 2:
        print("The list of current employees is: \n")
        for key in all_staff:
        which_remove = input("Now, which do you want to remove? Enter the username exactly.\n")
        if which_remove in managers:
            del managers[which_remove]
            del employees[which_remove]
        print("\nDone. Updated roster is:\n")
        all_staff = dict(managers, **employees)
        for key in all_staff:



source to share

2 answers

There are several ways to read and parse your input file. Assuming your file is listed by you and you're ready to handle exceptions, here's one example method. You need to handle exceptions accordingly.

    #This will read your in.conf which contains user/pwds in the dictionary format you specified.
    #Read documentation on exec here: 
    with open('in.conf') as fh:
        for line in fh:
    user_id = input("User ID:\n")
    user_password = input('Password:\n')
    if user_id in managers and user_password == managers[user_id]:
        print("Welcome, {0}.".format (user_id))
    elif user_id in employees and user_password == employees[user_id]:
        print("Welcome, {0}.".format (user_id))
        print "invalid credentials"
except NameError:
    #catch situations where your file doesn't contain managers or employees dictionary
    #I just raise it so you can see what it would print
    #other exceptions as you see appropriate to handle ....
    #I just raise it so you can see what it would print




The lines are a readline

little wrong. The argument readlines

is the maximum number of bytes it will read. Thus, it readlines(6)

does not mean "read the sixth line", it means "read no more than six characters from the current line". I suggest to just do it read_employee_file.readline()

with no arguments.

managers = read_employee_file.readline()
employees = read_employee_file.readline()


You now have the complete content of each line, but both variables are still strings. However, you can use the module json

to load these dictionaries.

import json
line = 'managers = {"manager":"password", "owner":"apple"}'

#chop off the left part of the string containing "managers = "
line = line.partition(" = ")[2]
d = json.loads(line)

print "the owner password is", d["owner"]

if "bob" not in d:
    print "can't find bob password in managers dictionary!"



the owner password is apple
can't find bob password in managers dictionary!




All Articles