I wrote this login program as part of my project for school. Should I use my implementation of a login program, or is there some standardized patterns of login programs, like the ones in encryption where we use made packages instead of writing our own?

Also, please correct me if am doing anything wrong or if there is a faster and more elegant way.

users = []

    Look of data.txt



def loadData():
    with open('data.txt','r') as data:
        for line in data:
            user = makeUser(line)
    return True

def saveData():
    with open('data.txt','w') as data:
        for user in users:
            print(user['name']+'|'+user['surname']+'|'+user['username']+'|'+user['password'],file = data)

def makeUser(line):
    name, surname, username, password = line.split('|')
    if password[-1:] == '\n':
        password = password[:-1]
    return {'name':name, 

def register():
    name = input('Name:')
    surname = input('Surname:')
    while True:
        username = input('Username:')
        if checkLen(username):

    while True:
        password = input('Password:')
        if checkLen(password):

def checkLen(info):
    if len(info) > 0:
        return True
        print('Can\'t be blank!')

def login(state):
    while state:
        username = input('Username:')
        password = input('Password:')
        for user in users:
            if user['username'] == username and user['password'] == password:
                print('You are logged in.')
                state = False
            print('Wrong input.')

def main():
    state = loadData()

    print('1) Login')
    print('2) Register')
    print('x - Exit')
    choice = input('>')

    while choice not in ['1','2','x']:
        choice = input('>')

    if choice == '1':
    elif choice == '2':
    elif choice == 'x':

        if __name__ == '__main__':

1 Answer 1


The biggest problem you currently have is keeping the passwords in plain text - use a one-way irreversible hash function, like argon2 or bcrypt.

Going even further, you should not be doing it yourself and instead trust something stable that have already proved itself of being mature and safe - like passlib library.

If you know that user authentication part is going to become more complicated, think about using a specialized User Directory like LDAP or Active Directory - there is a third-party ldap package that you can use for your client implementation.

Also, when a password is entered, the input should be hidden - there is a getpass module specifically for this kind of use case. Also, look into getuser() function for the login prompt.

There are also different third-party libraries, like click or cement that might help in making building the CLI apps easier. Explore the other CLI libraries at the awesome-python list.

  • \$\begingroup\$ Thanks for answer, passlib library and getpass module are awesome... \$\endgroup\$
    – Newbie
    Commented Feb 4, 2017 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.