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)
print(managers)
read_employee_file.close()

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))
        user_status='manager'
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
        else:
            new_staff_username = input("Enter the new staff member username...\n")
            new_staff_password = input("Enter the new staff member password...\n")
            employees[new_staff_username]=new_staff_password

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

      

+3


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.



try:
    #This will read your in.conf which contains user/pwds in the dictionary format you specified.
    #Read documentation on exec here: 
       # https://docs.python.org/3.0/library/functions.html#exec
    with open('in.conf') as fh:
        for line in fh:
            exec(line)
    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))
        user_status='manager'
    elif user_id in employees and user_password == employees[user_id]:
        print("Welcome, {0}.".format (user_id))
        user_status='staff'
    else:
        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
    raise
except:
    #other exceptions as you see appropriate to handle ....
    #I just raise it so you can see what it would print
    raise

      

0


source


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!"

      

Result:

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

      

0


source







All Articles