Python Forum
delete line from a text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
delete line from a text file
#1
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()
Reply


Messages In This Thread
delete line from a text file - by thesisonews - Mar-20-2018, 06:59 PM
RE: delete line from a text file - by Gribouillis - Mar-20-2018, 08:18 PM
RE: delete line from a text file - by nilamo - Mar-20-2018, 08:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 2,205 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
  Delete file with read-only permission, but write permission to parent folder cubei 6 26,209 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 15,860 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 1,607 Jan-31-2023, 09:29 PM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 2,169 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 4,116 Sep-27-2022, 01:38 PM
Last Post: buran
  Graphic line plot with matplotlib, text file in pytho khadija 2 2,718 Aug-15-2022, 12:00 PM
Last Post: khadija
  Delete multiple lines from txt file Lky 6 4,303 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Delete empty text files [SOLVED] AlphaInc 5 3,505 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  Modify values in XML file by data from text file (without parsing) Paqqno 2 3,359 Apr-13-2022, 06:02 AM
Last Post: Paqqno

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020