简体   繁体   English

从我用来存储登录用户名和密码的文本文件顶部读取

[英]Read from top of text file that I use to store login username and password

My "user.txt" contains admin username and password(admin, admin).我的“user.txt”包含管理员用户名和密码(admin,admin)。 I use it to log in. Once I register a new user into the text file, the admin username and password don't work anymore, but I can log in using the new user credentials I have just created.我用它来登录。一旦我在文本文件中注册了一个新用户,管理员用户名和密码就不再起作用,但我可以使用我刚刚创建的新用户凭据登录。 Please help.请帮忙。

importing current date
from datetime import datetime
import csv
#opening user file

user_file = open("user.txt", "r+")
text = user_file.readlines()

login = False

#while login is false,
#if user enters correct credentials, login
#changes to True. Giving excess

while login == False:

    username = input("Enter your username: ")
    password = input("Enter your password: ")

    for lines in text:
        valid_user, valid_password = lines.split(", ")
        valid_user, valid_password.strip()

    if valid_user == username and valid_password == password:
        login = True
        print("Logging in...")
    if valid_user != username and valid_password != password:
        print("Invalid login details")
    user_file.seek(0)
user_file.close()

At the end of the loop the valid_XXX variables just contain the last line.在循环结束时, valid_XXX变量只包含最后一行。

You need to compare each line in the loop.您需要比较循环中的每一行。

for lines in text:
    valid_user, valid_password = map(str.strip, lines.split(", "))
    
    if valid_user == username and valid_password == password:
        login = True
        print("Logging in...")
        break
    else:
        print("Invalid login details")

The else: block of a loop is executed if the loop finishes normally instead of with a break statement.如果循环正常结束而不是使用break语句,则执行else:循环块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何循环输入密码/用户名登录页面并从外部文件读取密码/用户名? - How do I loop my password/username login page and read the password/username from an external file? 从文本文件中读取用户名和密码 - read username and password from text file 从文本文件中获取用户名:密码,然后发布到登录表单 - Grab username:password from text file, then post into login form 通过搜索用户名从文件中获取密码并将该密码存储在变量中 - Grab password from a file by searching username and store that password in variable 从文本文件中检查用户名和密码 - Checking a username and password from a text file 来自 csv 而不是文本文件的用户名和密码 - username and password from csv instead of text file python使用pickle文件存储用户名和密码是否安全? - is it secure for python use pickle file to store the username and password? 如何使用Selenium登录需要用户名和密码的站点? - How do I use Selenium to login to sites that require username and password? 用户名和密码未保存到文本文件 - Username and Password not saving to text file 在html页面中填写注册表单后,它会出现此错误并使用用户名和密码并以登录表单存储值 - After filling signup form in html page it gives this error and use username and password and store values in login form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM