Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a Python Log-In system using text files

I’m trying to create a basic Sign-up and Sign-in system in python using text files. I also want the program to run in one go, meaning I want the user to be able to signup, and then proceed to log in right away, instead of having them go back and run the code again. The code that I have written though, can’t do that for some reason. I’ve added "signin()" at the end of the sign up function to allow the users to proceed to sign in after a successful registration, but doing so throws an error. Instead I have to run the code again for the new signup to be recognized.

Here’s the code:

def signin():
    q = 0
    user = input('Username: ')
    password = input('Password: ')
    with open('username.txt', mode='r')as names:
        for i in names:
            foundname = names.readlines()
    with open('password.txt', mode='r')as passwords:
        for j in passwords:
            foundpass = passwords.readlines()
    if user + "\n" in foundname:  # ReadLines() adds newlines
        user_num = foundname.index(user + "\n")
        for i in range (0,user_num):
            if password + "\n" == foundpass[i]:
                print("Login Successful.")
        print("Incorrect Password. Try Again.")
        signin()

    else:
        print("Username not found. Try again")
        signin()

def signup():
    with open('username.txt', mode='a')as names:
        print("Please provide the following details to sign up")
        name = str(input("Username: "))
        names.write(f"{name}\n")
    with open('password.txt', mode='a')as passwords:
        password = (input("Password: "))
        passwords.write(f'{password}\n')
        print("signup successful. You can sign-In Now.")
        signin()
signup()

Here’s the output and the error I keep getting:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Please provide the following details to sign up
Username: abcdef
Password: password1
signup successful. You can sign-In Now.
Username: abcdef
Password: password1
Traceback (most recent call last):
  File "D:/student.py", line 34, in <module>
    signup()
  File "D:/student.py", line 33, in signup
    signin()
  File "D:student.py", line 14, in signin
    if password + "\n" == foundpass[i]:
IndexError: list index out of range

Process finished with exit code 1

But when I run the code again, the credentials I used to sign up the last time seem to work.

Please provide the following details to sign up
Username: jkl
Password: mno
signup successful. You can sign-In Now.
Username: abcdef
Password: password1
Login Successful.

Thanks a lot in advance!

>Solution :

with open('password.txt', mode='a')as passwords:
        ...
        signin()

The file doesn’t close, and you attempt to sign in, because of which the file is pointing at the last index and you can’t read any data. Changing it to

with open('password.txt', mode='a')as passwords:
    password = (input("Password: "))
    passwords.write(f'{password}\n')
    print("signup successful. You can sign-In Now.")
signin()

should fix the issue.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading