Python Forum

Full Version: delete line from a text file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i been trying for hours, no luck

the function ask the user what name he want to delete from the text file

def Delete_con():
    with open('phonebook1.txt', 'r+') as f:
        t = f.read()
        to_delete = input('What should we delete? : ').strip()
        f.seek(0)
        for line in t.split('\n'):
            if line != to_delete:
                f.write(line)
        f.truncate()
        return Menu()
Perhaps replace line 8 with print(line, file=f)
What errors do you get?