【发布时间】:2018-08-05 04:56:37
【问题描述】:
我试图让 python 完整地读取我的 .txt 文件,以便确定用户名/密码的有效性……但它似乎只读取第一行而忽略其余部分。只有 .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
这一切都通过了:
def accessgranted():
credentialcheck.delete(0.0, END)
userandpass=login()
if userandpass == True: quit()
.txt 文件:
abc-123
test-test
user-pass
【问题讨论】:
-
请从
if结构中删除return True。还可以使用生成器从文件中读取效率更高!