Search code examples
pythonpython-3.xrecovery

reading file problem with python username and passowrd recovery system


This Is a recovery system where the user enters their unique code to recover their details. when the unique code is entered it takes their details from a text file and then it prints their username and password. Then they are given a choice to change their password. The problem that I have is that when I type the old password in it sayes that the password is not in that specific line. The format in the file is this:

(unique code) username: (username) password: (password)

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

So when I enter the unique code it shows the right line then when I try changing the password it keeps on saying the password is not in that specific line.


Solution

  • Currently, when looping through lines in the text file, if hash1 is not in ANY line, it will tell you that the code is not found, and immediately break.

    To fix this, you should break or return from inside your if hash1 in text conditional, then test if len(hash1) < 32 and (check == True) which will now only run AFTER checking all the lines in the file.

    Also, supposing text is 'aslhngbadf username: bob password: alice', line.strip().split() will be ['aslhngbadf', 'username:', 'bob', 'password:', 'alice'], so checking that password1 is in text won't work for one letter of the password. It will, however, work if you enter the word 'password:' or 'bob'... , check that things match explicitly like if password1 == text[4].

    Finally, when you reopen and itterate over the file, you move from the line where your text matched, back to the top of the file. Don't open mount.txt twice. text still remains available and unchanged from the first time you set it.