Error writing text into a .txt file in python
Question: I am making a program that will 1\. Create a text file 2\. Allow a password to
be stored 3\. Allow a password to be changed 4\. Add an additional password
5\. Delete a specific password The problem is in `def delete():`. I put in
three passwords on three seperate lines: first, second, third. When I choose
to delete password "second", it reprints the list from before, and then prints
the new list at the end of the last password.

Here is my code:

    
    
    import time
    def create():
        file = open("password.txt", "w")
        passwordOfChoice = input("The password you want to store is: ")
        file.write(passwordOfChoice)
        print ("Your password is: ", passwordOfChoice)
        file.close()
        time.sleep(2)
    def view():
        file = open("password.txt","r")
        print ("Your password is: ",
               "\n", file.read())
        file.close()
        time.sleep(2)
    def change():
        file = open("password.txt", "w")
        newPassword = input("Please enter the updated password: ")
        file.write(newPassword)
        print ("Your new password is: ", newPassword)
        file.close()
        time.sleep(2)
    def add():
        file = open("password.txt", "a")
        extraPassword = input("The password you want to add to storage is: ")
        file.write("\n")
        file.write(extraPassword)
        print ("The password you just stored is: ", extraPassword)
        file.close()
        time.sleep(2)
    def delete():
        phrase = input("Enter a password you wish to remove: ")
    
        f = open("password.txt", "r+")
        lines = f.readlines()
    
        for line in lines:
            if line != phrase+"\n":
                f.write(line)
        f.close()
    
    print("Are you trying to: ",
          "\n1. Create a password?",
          "\n2. View a password?",
          "\n3. Change a previous password?",
          "\n4. Add a password?",
          "\n5. Delete a password?",
          "\n6. Exit?\n")
    function = input()
    print("")
    
    if (function == '1'):
        create()
    elif (function == '2'):
        view()
    elif (function == '3'):
        change()
    elif (function == '4'):
        add()
    elif (function == '5'):
        delete()
    elif (function == '6'):
        print("Understood.", "\nProgram shutting down.")
        time.sleep(1)
    else:
        print("Your answer was not valid.")
        print("Program shutting down...")
        time.sleep(1)
    

To show what I meant above, here is my output:

    
    
    Your password is:
     first
    second
    thirdfirst
    third
    

Can someone please tell me how to fix my `def delete():` function so that it
will not rewrite the original data? Thanks a ton!


Answer: The problem lies with the 'r+' mode. When you use 'r+' you can read and write,
sure, but it's up to you to control _where_ in the file you write. What's
happening is you read the file, and the cursor is stuck at the end, so when
you write it back, Python dutifully puts your new lines on the end of the
file. See [the
docs](https://docs.python.org/3.4/tutorial/inputoutput.html#methods-of-file-
objects) about file methods; you're looking for something like **seek**.

