如何检查文本文件中的用户名,然后要求输入密码?


loginUsername = input("Enter Username: ")
loginPassword = input("Enter PASSWORD: ")
data=open('database.txt', 'r')
accounts = data.readlines()
for line in data:
accounts = line.split(",")
if (loginUsername == accounts[0] and loginPassword == accounts[1]):
print("LOGGED IN")
else:
print("Login FAILED")
print(accounts)

我想做一个文本登录系统,这将要求用户名第一。检查保存用户名和密码的文本文件后,系统会要求输入密码。但是我不知道如何阅读第一列(这是用户名,文本文件的结构是&;用户名,密码&;)。如果我使用readlines()和split(",")但也有"不";在密码末尾。

# You should always use CamelCase for class names and snake_case
# for everything else in Python as recommended in PEP8.
username = input("Enter Username: ")
password = input("Enter Password: ")
# You can use a list to store the database's credentials.
credentials = []
# You can use context manager that will automatically
# close the file for you, when you are done with it.
with open("database.txt") as data:
for line in data:
line = line.strip("n")
credentials.append(line.split(","))
authorized = False
for credential in credentials:
db_username = credential[0]
db_password = credential[1]
if username == db_username and password == db_password:
authorized = True
if authorized:
print("Login Succeeded.")
else:
print("Login Failed.")

密码末尾可能是换行符n。为了删除它,您可以使用rstrip()函数:

mystring = "passwordn"
print(mystring.rstrip())
>>> 'password'

相关内容

最新更新