简体   繁体   English

Python-无法读取整个.txt文件:.readlines错误?

[英]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. 我试图让python完整读取我的.txt文件,以便确定用户名/密码的有效性...但是它似乎只读取第一行,而忽略其余的行。 Only the first username/password on the .txt will grant access. .txt上只有第一个用户名/密码将授予访问权限。

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: .txt文件:

abc-123
test-test
user-pass

Because you are returning in each branch of the 'if' statement. 因为您要在“ if”语句的每个分支中返回。 Returning True in the if condition seems okay, but remove the other return statement from the loop. 在if条件中返回True似乎还可以,但是请从循环中删除另一个return语句。 When the loop ends without returning True for any of the entries in the file, it means the credentials entered are not valid. 如果循环结束而文件中的任何条目均未返回True,则表示输入的凭据无效。 Then you can return False. 然后,您可以返回False。

You're short-circuiting your loop by returning False immediately if the first line doesn't match. 如果第一行不匹配,则通过立即返回False来缩短循环。 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. in check也不正确,我可以使用用户名= ab和pw = 1成功登录,尽管这不是完整的用户名或密码。

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

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()) . 因此,该错误不是由于database.readlines()无效导致的,这是因为print(database.readlines())之后的代码。

And the database.readlines() becomes the [] ! 并且database.readlines()成为[]

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. 这是因为在第一次分配readlines方法之后,文件光标指向文件的末尾。因此,除非将文件光标更改为文件的开头,否则我们将无法再读取任何字符。

And your problem is the returning after every time the if-case sentence finished! 而您的问题是每次if-case语句结束后的返回! 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM