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

标签 python python-3.x recovery

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

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

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/

相关文章:

Python:如何同时发送多个http请求? (像 fork )

python - 使用 pymongo 将自定义 python 对象编码为 BSON

Python: "NameError: name ... is not defined"环境中的奇怪 'exec'

java - 当 Java.util.logging 的 FileHandler 无法写入文件时,它会做什么?

android - android 中的/cache/recovery 文件夹

python - 如何在 Python 中并行化列表理解计算?

Python 3 : How to remove items from an inventory in a text-based game?

python - 如何在for循环中从字典中获取单个值

python - 自定义字典在施放时丢弃键

git - 如何恢复 Git 中丢失的存储?