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

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

我试图让 python 完整阅读我的 .txt 文件,以确定 username/password 的有效性......但它似乎只阅读了第一行和忽略其余部分。只有 .txt 上的第一个 username/password 将被 g运行t 访问。

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

这就是全部 运行 通过这个:

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

.txt 文件:

abc-123
test-test
user-pass

因为您在 'if' 语句的每个分支中 returning。在 if 条件中返回 True 似乎没问题,但从循环中删除其他 return 语句。当循环结束时文件中的任何条目都没有 return 为 True,这意味着输入的凭据无效。那你可以return假。

如果第一行不匹配,您会通过立即返回 False 来缩短循环。取消缩进这些行,以便在循环完成后调用它。 in 检查也不正确,我可以使用用户名 = 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

为了简化代码,在不影响结果的情况下做了一些修改

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()

为了检查读取行方法的总结果,我打印了从文件中读取的列表。

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()

输出:

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

所以bug不是因为database.readlines()没有起作用,而是因为print (database.readlines()).

database.readlines() 变成 []!

那是因为我们在第一次分配readlines方法后,文件光标指向文件末尾time.Therefore我们不能再读取任何字符,除非将文件光标更改为文件开头。

而您的问题是每次 if-case 语句结束后 returning!修改你的return false语句的缩进后,如果没有匹配到用户名和密码,就会赋值,问题就解决了!

现在我们只修改代码:

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()