如何删除文本文件中包含某些单词的行

时间:2021-01-04 22:30:58

标签: python text

我正在制作一个密码管理器,密码存储在一个文本文件中,我正在尝试实现根据使用密码的站点删除某些密码的功能,所以我想问的主要问题是如果行满足特定要求(站点名称),我如何删除文本文件中的特定行。我将不胜感激任何帮助。这是我的代码。

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 个答案:

答案 0 :(得分:0)

您可以先读取所有行,然后将它们写回到文件中,但如果遇到要删除的行,则不要写入。您可以使用诸如正则表达式之类的东西进行匹配:

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)