gpt4 book ai didi

python - python用户名和密码恢复系统读取文件问题

转载 作者:太空宇宙 更新时间:2023-11-03 21:20:08 25 4
gpt4 key购买 nike

这是一个恢复系统,用户可以在其中输入其唯一代码来恢复其详细信息。当输入唯一代码时,它会从文本文件中获取他们的详细信息,然后打印他们的用户名和密码。然后他们可以选择更改密码。我遇到的问题是,当我输入旧密码时,它说密码不在该特定行中。文件中的格式是这样的:

(唯一代码)用户名:(用户名)密码:(密码)

global password1, password2,
check = True
while check:
hash1=input("\n"+"Enter your unique code to recover your username or password ")
with open("mount.txt","r") as file:
for line in file:
text = line.strip().split()
if hash1 in text:
print(line)
check = False
change = input("Do you want to change your password? [y,n]")
while change not in('y', 'n'):
change = input("Do you want to change your password? [y,n]")
if change == "y":
check = True
with open("mount.txt","r+") as file:
for line in file:
text = line.strip().split()
while check:
password1 = input("Enter old password ")
password2 = input("Enter new password ")
if len(password2) < 5:
print("New password is too short")
elif password1 not in text:
print("Old password is incorrect")
elif password1 in text:
s = open("mount.txt").read()
s = s.replace(password1, password2)
f = open("mount.txt", 'w')
f.write(s)
f.close()
print("Password has been successfully changed")
check = False

elif len(hash1) < 32 and (check == True):
print("Unique code not found please try again")
break

因此,当我输入唯一代码时,它会显示正确的行,然后当我尝试更改密码时,它会一直显示密码不在该特定行中。

最佳答案

当前,当循环文本文件中的行时,if hash1不在任何行中,它会告诉你没有找到代码,并立即中断。

要解决此问题,您应该从 if hash1 in text 内部中断或返回有条件的话,测试if len(hash1) < 32 and (check == True)现在只有在检查文件中的所有行之后才会运行。

此外,假设文本为“aslhngbadf 用户名:bob 密码:alice”,line.strip().split()将是['aslhngbadf', 'username:', 'bob', 'password:', 'alice'] ,所以检查 password1位于text对于密码中的一个字母不起作用。但是,如果您输入单词“password:”或“bob”...,检查内容是否明确匹配,如if password1 == text[4],它将起作用。 .

最后,当您重新打开并遍历文件时,您将从文本匹配的行移回到文件的顶部。不要打开 mount.txt 两次。 text自您第一次设置以来仍然保持可用且不变。

关于python - python用户名和密码恢复系统读取文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54354184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com