2

I'm creating a program to update a text file, that has a list of cities:

New York City
New York
USA

Newark
New Jersey
USA

Toronto
Ontario
Canada

If I wanted to delete the details for Newark using a bash script, I can do this:

sed -i "/Newark/,+3d" test.txt

And that would leave me with the following:

New York City
New York
USA

Toronto
Ontario
Canada

However, I would like to do this in Python, and I'm having issues figuring out how to delete the following lines, after the line with Newark. I can delete Newark:

with open('test.txt') as oldfile, open('test2.txt', 'w') as newfile:
        for line in oldfile:
            if not "Newark" in line:
                newfile.write(line)

os.remove('test.txt')
os.rename('test2.txt', 'test.txt')

But this does nothing for the remaining two lines, and creates a new file I then have to use to replace the original file.

  1. How can I go about mimicing the sed command's functionality with Python?
  2. Is there any way to do an in file edit, so I do not have to create and replace the file everytime I need to delete from it?
martineau
  • 119,623
  • 25
  • 170
  • 301
SVill
  • 331
  • 5
  • 22
  • 55

2 Answers2

3

With a counter ? Here it is:

with open('test.txt') as oldfile, open('test2.txt', 'w') as newfile:
    skip = 0
    for line in oldfile:
        if "Newark" in line:
            skip = 3
        elif skip > 0:
            skip = skip - 1
        else:
            newfile.write(line)

Edit:

I only answered the first question. fileinput support file editing, please have a look here: How to search and replace text in a file? Btw, I would recommend also in-place because it doesn't hijack stdout

Raphael Medaer
  • 2,528
  • 12
  • 18
0

You can use the fileinput module to edit files in place:

import fileinput
import itertools

skip = 3
with fileinput.input("test.txt", inplace=True) as f:
     for line in f:
         if "Newark" not in line:
             # stdout is redirected to the file
             sys.stdout.write(line)
         else:
             # Skip lines
             next(itertools.islice(f, skip, skip), None)
iz_
  • 15,923
  • 3
  • 25
  • 40