从文件创建列表时,如果文件包含多个值,它将无法识别任何值

2024-04-26 03:16:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我制作了一个程序,应该从文件中获取用户名和密码列表。如果文件只有一个用户名和密码,它就可以完美地工作,但一旦我包含了多个用户名和密码,它就根本无法识别它们。下面是代码

import easygui as e
import os
upper = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
lower = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
numbers = ["1","2","3","4","5","6","7","8","9","0"]
loginsystem = True
while loginsystem == True:
    users = []
    file = open("users.secure","r")
    reading = True
    while reading == True:
         tempuser = file.readline()
         if tempuser == "":
            reading = False
        else:
            users.append(tempuser)
    file.close()
    passwords = []
    file = open("passwords.secure","r")
    reading = True
    while reading == True:
        temppassword = file.readline()
        if temppassword == "":
            reading = False
        else:
            passwords.append(temppassword)
    file.close()
    loginmsg = "Enter your username and password"
    logintitle = "Login"
    loginfieldnames = ["Username","Password"]
    loginfieldvalues = []
    login = True
    while login == True:
        loginfieldvalues = 
e.multpasswordbox(loginmsg,logintitle,loginfieldnames)
        while 1:
            if loginfieldvalues == None: break
            loginerrmsg = ""
            for i in range(len(loginfieldnames)):
              if loginfieldvalues[i].strip() == "":
                loginerrmsg = loginerrmsg + ('"%s" is a required field.\n\n' %     loginfieldnames[i])
            if loginerrmsg == "": break
            loginfieldvalues = e.multpasswordbox(loginerrmsg, logintitle, loginfieldnames, loginfieldvalues)
        inputuser = loginfieldvalues[0]
        inputpassword = loginfieldvalues[1]
        if inputuser not in users:
            e.msgbox("Unknown username.",title="Login",ok_button="Try Again")
        else:
            placement = users.index(inputuser)
            if inputpassword != passwords[placement]:
                 e.msgbox("Invalid password.",title="Login",ok_button="Try Again")
             else: login = False
    e.msgbox("Welcome, " + inputuser,title="Login System",ok_button="Continue")
    basicoptions = ["Logout","Quit"]
    running = True
    while running == True:
        choice = e.buttonbox("What would you like to do?",title="Login System",choices=basicoptions)
        if choice == "Quit":
            os._exit(0)
        else:
            running = False

这些文件只包含单词“admin”,当我添加另一个值时,在写入文件时使用“\nadmin2”将其添加到下一行


Tags: falsetrueiftitleloginuserselsefile
1条回答
网友
1楼 · 发布于 2024-04-26 03:16:49

io.readline()将返回换行符。这意味着,如果您只有一个条目,则可能会得到admin,但如果行数更多,则可能会得到admin\n

相反,您可以:

tempuser = file.readline().strip()

与问题无关,但您可以大量清理代码。例如,用于读取文件:

def read_file(path):
    with open(path, 'r') as f:
        return [line.strip() for line in f]

users = read_file('users.secure')
passwords = read_file('passwords.secure')

相关问题 更多 >