Q:

Locate a specific line in a file based on user input then delete a specific number of lines

I'm trying to delete specific lines in a text file the way I need to go about it is by prompting the user to input a string (a phrase that should exist in the file) the file is then searched and if the string is there the data on that line and the number line number are both  stored. 
After the phrase has been found it and the five following lines are printed out. Now I have to figure out how to delete those six lines without changing any other text in the file which is my issue lol.
Any Ideas as to how I can delete those six lines?
This was my latest attempt to delete the lines
file = open('C:\\test\\example.txt', 'a')
locate = "example string"
for i, line in enumerate(file):
    if locate in line:
        line[i] = line.strip()
        i = i+1

        line[i] = line.strip()
        i = i+1

        line[i] = line.strip()
        i = i+1

        line[i] = line.strip()
        i = i + 1

        line[i] = line.strip()
        i = i+1

        line[i] = line.strip()
        break

A:

Usually I would not think it's desirable to overwrite the source file - what if the user does something by mistake?  If your project allows, I would write the changes out to a new file.
with open('source.txt', 'r') as ifile:
    with open('output.txt', 'w') as ofile:
        locate = "example string"
        skip_next = 0
        for line in ifile:
            if locate in line:
                skip_next = 6
                print(line.rstrip('\n'))
            elif skip_next > 0:
                print(line.rstrip('\n'))
                skip_next -= 1
            else:
                ofile.write(line)

This is also robust to finding the phrase multiple times - it will just start counting lines to remove again.

