2

I am trying create a login system. The user creates their account and inputs the username and password they would like to use.

The username gets stored in line 1 of a new text file.

The password gets stored in line 2 of the text file.

Now I open the file and make it readable

user_file = open("user1.txt", "r")

Then I have the user enter the username created earlier.

username_login = input("Enter username ")

Now I want check to see if the username submitted is the one stored on line 1

if user_file.readline(1) != username_login:
        print("Incorrect username")
    else:
        password_login = input("Enter Password ")

If the username submitted is correct it would match line 1 and therefore print the else statement. but instead I always get the "incorrect username" feedback

  • 3
    Obligatory comment about this being an incredibly insecure way to create a login system. You never want to store user credentials in plain text. If this is anything production and not just an academic exercise please look into hashing your passwords before you store them, and then comparing the hash value of the user's input with your stored hash value. – Ryan Kozak Feb 25 '20 at 22:24
  • Yes this an academic exercise, but I look forward to learning how to hash passwords when I am finished so i can improve the program – Sebastian Frasher Feb 26 '20 at 12:00

3 Answers3

2

Assuming the file consists of just 2 lines, what you could do is extract username and password and then compare them with input's data. Example:

with open('user1.txt') as f:
   username, password = f.read().splitlines() 
# now compare username and password with input data
abc
  • 11,579
  • 2
  • 26
  • 51
1

Go through your file with a for loop:

for line in user_file.readlines():
    if line[0] == username:
        #do something
1

As Ryan said, this is definitely not the way that logins should be managed in reality, but assuming this is just practicing python...

First, note that python indices start at 0, so the first line would be called line 0. I also don't believe readline() accepts a parameter to make it do what you want. Instead, one solution could be to store the file contents into a list which can be indexed:

lines = user_file.readlines()
username_login = input("Enter username ")
if lines[0].strip('\n') != username_login:
    print("Incorrect username")
else:
    password_login = input("Enter Password ")

and then to access the password you can do lines[1]

ba5h
  • 97
  • 1
  • 11
  • Okay that makes sense. I learned the number inside the readline() is to cap the byte buffer. Although I tried the method you recommended still returns "incorrect username" lines = user_file.readlines() username_login = input("Enter username ") if lines[0] != username_login: print("Incorrect username") else: password_login = input("Enter Password ") – Sebastian Frasher Feb 25 '20 at 23:39
  • 1
    Oh, sorry, I forgot that reading lines like this also includes the newline character at the end of the line. I have edited the code to remove it so it now works – ba5h Feb 26 '20 at 00:12