at8k
at8k

Reputation: 1

Python - Can't Read Entire .txt File: .readlines error?

I'm trying to have python read my .txt file in its entirety in order to establish the validity of a username/password... But it seems to only read the first line and ignore the rest. Only the first username/password on the .txt will grant access.

def login():
username = textentry.get()
password = textentry2.get()
database=open('database.txt')
for line in database.readlines():
    usr, pas = line.strip().split("-")
    if (username in usr) and (password in pas):
        credentialcheck.insert(END, "welcome")
        return True
    credentialcheck.insert(END, "username or password incorrect")
    return False

Which is all ran through this:

def accessgranted():
    credentialcheck.delete(0.0, END)
    userandpass=login()
    if userandpass == True: quit()

The .txt file:

abc-123
test-test
user-pass

Upvotes: 0

Views: 1257

Answers (3)

Sнаđошƒаӽ
Sнаđошƒаӽ

Reputation: 17562

Because you are returning in each branch of the 'if' statement. Returning True in the if condition seems okay, but remove the other return statement from the loop. When the loop ends without returning True for any of the entries in the file, it means the credentials entered are not valid. Then you can return False.

Upvotes: 1

Saige Zhang
Saige Zhang

Reputation: 787

In order to simplify the code, I did some modification without affecting the result.

def login():
  username = "test"
  password = "test"
  database=open('database.txt')
  for line in database.readlines():
     usr, pas = line.strip().split("-")
     if (username in usr) and (password in pas):
         print ("welcome")
         return True
     print ("error")
     return False
login()

In order to check the totally result of reading lines method, I printed the lists read from file.

def login():
    username = "test"
    password = "test"
    database=open('database.txt')
    print (database.readlines())
    for line in database.readlines():
        print (line)
        usr, pas = line.strip().split("-")
        print (usr,pas)
        if (username in usr) and (password in pas):
            print ("welcome")
            return True
        print ("error")
        return False
login()

The output:

['abc-123                                                                 \n', 'test-test                                                               \n', 'user-pass']

Therefore the bug is not due to database.readlines() didn't work, it's because the codes after print (database.readlines()).

And the database.readlines() becomes the []!

That's because the file cursor pointed to the end of file after we assign the readlines method at the first time.Therefore we can't read any more characters unless change the file cursor to the beginning of file.

And your problem is the returning after every time the if-case sentence finished! After modifying your return false sentence's indentation,which will be assigned if no username and password can be matched,the problem will be solved!

Now we just modify the code:

def login():
    username = "test"
    password = "test"
    database=open('database.txt')
    print (database.readlines())
    print (database.readlines())
    for line in database.readlines():
        print (line)
        usr, pas = line.strip().split("-")
        print (usr,pas)
        if (username in usr) and (password in pas):
            print ("welcome")
            return True
    print ("error")
    return False
login()

Upvotes: 0

saarrrr
saarrrr

Reputation: 2877

You're short-circuiting your loop by returning False immediately if the first line doesn't match. Unindent those lines so it's called after the loop completes. The in check is incorrect as well, I could successfully log into this with username = ab and pw = 1, although that's not the full username or password.

def login():
    username = textentry.get()
    password = textentry2.get()
    database=open('database.txt')
    for line in database.readlines():
        usr, pas = line.strip().split("-")
        if (username == usr) and (password == pas):
            credentialcheck.insert(END, "welcome")
            return True
    credentialcheck.insert(END, "username or password incorrect")
    return False

Upvotes: 0

Related Questions