How do I delete multiple lines in a text file with python?

Chris Jones

I am practicing my python skills by writing a Phone book program. I am able to search and add entries but I am having a lot of trouble deleting entries.

I am trying to find a line that matches my search and delete it as well as the next 4 lines. I have a function to delete here:

def delete():
    del_name = raw_input("What is the first name of the person you would like to delete? ")
    with open("phonebook.txt", "r+") as f:
            deletelines = f.readlines()
            for i, line in enumerate(deletelines):
                if del_name in line:
                    for l in deletelines[i+1:i+4]:
                        f.write(l)

This does not work.

How would I delete multiple entries from a text file like this?

roippi

Answering your direct question: you can use fileinput to easily alter a text file in-place:

import fileinput
file = fileinput.input('phonebook.txt', inplace=True)

for line in file:
     if word_to_find in line:
         for _ in range(4): # skip this line and next 4 lines
             next(file, None)
     else:
         print line,

In order to avoid reading the entire file into memory, this handles some things in the background for you - it moves your original file to a tempfile, writes the new file, and then deletes the tempfile.

Probably better answer: it looks like you have rolled a homemade serialization solution. Consider using a built-in library like csv, json, shelve, or even sqlite3 to persist your data in an easier-to-work-with format.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I limit (or truncate) text file by number of lines?

From Dev

How to delete multiple lines in a python console window?

From Dev

Python - delete blank lines of text at the end of the file

From Dev

How do I write in a new text file every x lines? [python]

From Dev

How do I extract a range of lines from a text file using sed -n but in Python?

From Dev

How do I count the number of blank lines in a text file?

From Dev

How to delete specific lines in text file?

From Dev

In a text file, how do I delete all lines subset of their immediately following line?

From Dev

how can i do multiple lines of an input field? PYTHON

From Dev

How can I delete multiple random lines from a text file using sed?

From Dev

How can I delete all unordered lines from a text file?

From Dev

Delete multiple lines in a file

From Dev

Delete lines from a text file that match multiple regexes on a text file

From Dev

How do I modify a text file in Python

From Dev

Delete lines from text file - python

From Dev

Python - Parsing multiple lines of text from file

From Dev

How do I add / delete a line from a text file?

From Dev

How do I delete specific text in multiple cells at once?

From Dev

How do I write in a new text file every x lines? [python]

From Dev

How can I delete blank lines in a file?

From Dev

How do I sort a text file in python?

From Dev

Delete all lines in file that do not match text from another file

From Dev

How to delete specific lines in text file?

From Dev

How do I grab the first 10% of lines in a text file?

From Dev

Returning multiple lines from a text file in python

From Dev

Python: text file replace different strings in multiple lines HOW?

From Dev

Python - How do I separate data into multiple lines

From Dev

Python 2.7 - How do I remove lines in a text file longer than x amount?

From Dev

How do I delete lines matching multiple pattern in text file using bash command?

Related Related

  1. 1

    How do I limit (or truncate) text file by number of lines?

  2. 2

    How to delete multiple lines in a python console window?

  3. 3

    Python - delete blank lines of text at the end of the file

  4. 4

    How do I write in a new text file every x lines? [python]

  5. 5

    How do I extract a range of lines from a text file using sed -n but in Python?

  6. 6

    How do I count the number of blank lines in a text file?

  7. 7

    How to delete specific lines in text file?

  8. 8

    In a text file, how do I delete all lines subset of their immediately following line?

  9. 9

    how can i do multiple lines of an input field? PYTHON

  10. 10

    How can I delete multiple random lines from a text file using sed?

  11. 11

    How can I delete all unordered lines from a text file?

  12. 12

    Delete multiple lines in a file

  13. 13

    Delete lines from a text file that match multiple regexes on a text file

  14. 14

    How do I modify a text file in Python

  15. 15

    Delete lines from text file - python

  16. 16

    Python - Parsing multiple lines of text from file

  17. 17

    How do I add / delete a line from a text file?

  18. 18

    How do I delete specific text in multiple cells at once?

  19. 19

    How do I write in a new text file every x lines? [python]

  20. 20

    How can I delete blank lines in a file?

  21. 21

    How do I sort a text file in python?

  22. 22

    Delete all lines in file that do not match text from another file

  23. 23

    How to delete specific lines in text file?

  24. 24

    How do I grab the first 10% of lines in a text file?

  25. 25

    Returning multiple lines from a text file in python

  26. 26

    Python: text file replace different strings in multiple lines HOW?

  27. 27

    Python - How do I separate data into multiple lines

  28. 28

    Python 2.7 - How do I remove lines in a text file longer than x amount?

  29. 29

    How do I delete lines matching multiple pattern in text file using bash command?

HotTag

Archive