-2

I am making a password manager and the passwords are stored in a text file and I am trying to implement the ability to delete certain passwords according to the site the password is used in, so the main question I am trying to ask is how can I remove a specific line in a text file if the line meets a specific requirement (The name of the site). I would appreciate any help. Here is my code.

start = open("One.txt","r")


def editpassword():
    pass

def createpassword():
    passwordlist = open("PasswordList.txt", "a")
    print("What Is The Password You Would Like To Input?")
    newpassword = input("Password:")
    print("What Is The Name Of The Site Linked To This Password")
    newsite = input("Site:")
    passwordlist.write("Site:"+newsite)
    passwordlist.write(" Password:"+newpassword+"\n")
    passwordlist.close()
    passwordmanager()

def viewpassword():
    passwordlist = open("PasswordList.txt", "r")
    print("What Is The Site You Would Like The Password For?")
    site = input("Site:")
    for line in passwordlist:
        if site in line:
            print("Password Found\n"+ line)
    passwordlist.close()
    passwordmanager()

def deletepassword():
    passwordlist = open("PasswordList.txt", "r")
    print("What Is The Site You Would Like To Delete The Password For?")
    site = input("Site:")
    for line in passwordlist:
        if site in line:
            passwordlist = open("PasswordList.txt", "a")
            line.strip()


    passwordlist.close()
    passwordmanager()

def passwordmanager():
    choice = input("Would You Like To EDIT, CREATE, VIEW or DELETE A Password\n")
    if choice == "EDIT":
        editpassword()
    elif choice == "CREATE":
        createpassword()
    elif choice == "VIEW":
        viewpassword()
    elif choice == "DELETE":
        deletepassword()


def passwordcheck():
    input("Welcome to Password Manager Press ENTER To Start")
    while True:
        password = input("Please Enter Your Password:")
        passcode = open("password.txt", "r")
        if password == passcode.read():
            print("Password Was Correct")
            passwordmanager()
        else:
            print("Password Was Incorrect")

if start.read() != "Done":
    input("Welcome to Password Manager Press ENTER To Start")
    print("When First Starting You Need To Make A Password To Access Other Passwords")
    while True:
        password = input("Password:")
        password1 = input("Confirm Password:")
        if password == password1:

            print(password1 + " Is Now Your Password For Password Manager")
            passcode = open("password.txt","w")
            passcode.write(password1)
            passcode.close()

            start = open("One.txt", "w")
            start.write("Done")
            start.close()
            passwordcheck()
        else:
            print("Please Make Sure Both Passwords Are The Same")
else:
    passwordcheck()

1 Answers1

0

You can first read all the lines and then write them back to the file but if you come across the line that you want to delete, you just don't write it. You can use something like regex for the matching:

with open("PasswordList.txt", "r") as f:
    lines = f.readlines()
with open("PasswordList.txt", "w") as f:
    pattern = re.compile("<your regex goes here>")
    for line in lines:
        if not pattern.search(line.strip("\n")):
            f.write(line)
mandulaj
  • 733
  • 3
  • 10