Python 如何检查文本文件中的用户名和密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46738966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:48:22  来源:igfitidea点击:

How to check text file for usernames and passwords

python

提问by OSG

I'm writing a program which will need a user to register and login with an account. I get the program to have the user make their username and password which are saved in an external text file (accountfile.txt), but when it comes to the login I have no idea how to get the program to check if what the user inputs is present in the text file.

我正在编写一个程序,该程序需要用户注册并使用帐户登录。我让程序让用户创建他们的用户名和密码,这些用户名和密码保存在外部文本文件 (accountfile.txt) 中,但是在登录时我不知道如何让程序检查用户输入的内容存在于文本文件中。

This is what my bit of code looks like :

这是我的代码的样子:

def main():
    register()

def register():
    username = input("Please input the first 2 letters of your first name and your birth year ")
    password = input("Please input your desired password ")
    file = open("accountfile.txt","a")
    file.write(username)
    file.write(" ")
    file.write(password)
    file.close()
    login()

def login():
    check = open("accountfile.txt","r")
    username = input("Please enter your username")
    password = input("Please enter your password")

I have no idea what to do from this point.

从这一点上我不知道该怎么做。

Also, this is what a registered account would look like in the text file:

此外,这是注册帐户在文本文件中的样子:

Ha2001 examplepassword

回答by Brenden Petersen

After opening the file, you can use readlines()to read the text into a list of username/password pairs. Since you separated username and password with a space, each pair is string that looks like 'Na19XX myPassword', which you can split into a list of two strings with split(). From there, check whether the username and password match the user input. If you want multiple users as your TXT file grows, you need to add a newline after each username/password pair.

打开文件后,您可以使用readlines()将文本读入用户名/密码对列表。由于您用空格分隔了用户名和密码,因此每一对都是类似于 的字符串,'Na19XX myPassword'您可以将其拆分为两个字符串的列表split()。从那里,检查用户名和密码是否与用户输入匹配。如果随着 TXT 文件的增长需要多个用户,则需要在每个用户名/密码对后添加换行符。

def register():
    username = input("Please input the first 2 letters of your first name and your birth year ")
    password = input("Please input your desired password ")
    file = open("accountfile.txt","a")
    file.write(username)
    file.write(" ")
    file.write(password)
    file.write("\n")
    file.close()
    if login():
        print("You are now logged in...")
    else:
        print("You aren't logged in!")

def login():
    username = input("Please enter your username")
    password = input("Please enter your password")  
    for line in open("accountfile.txt","r").readlines(): # Read the lines
        login_info = line.split() # Split on the space, and store the results in a list of two strings
        if username == login_info[0] and password == login_info[1]:
            print("Correct credentials!")
            return True
    print("Incorrect credentials.")
    return False