繁体   English   中英

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

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

我试图让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”语句的每个分支中返回。 在if条件中返回True似乎还可以,但是请从循环中删除另一个return语句。 如果循环结束而文件中的任何条目均未返回True,则表示输入的凭据无效。 然后,您可以返回False。

如果第一行不匹配,则通过立即返回False来缩短循环。 取消缩进这些行,以便在循环完成后调用它。 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

为了简化代码,我做了一些修改而不影响结果。

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']

因此,该错误不是由于database.readlines()无效导致的,这是因为print(database.readlines())之后的代码。

并且database.readlines()成为[]

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

而您的问题是每次if-case语句结束后的返回! 修改返回的假句子缩进后,如果无法匹配用户名和密码,则会分配该缩进,此问题将得到解决!

现在我们只需修改代码:

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