I have been removing the desired lines from a text file by reading it in and rewriting each line if the string(s) I wish to remove are not present, using the following.
with open('infile.txt', 'r') as f:
lines = f.readlines()
with open('outfile.txt', 'w+') as f:
for line in lines:
if line.strip("\n") != "Desired text on line to remove":
f.write(line)
This works fine for all but one of the lines I need to remove which only contains.
1.
This is the first instance of (1.) in the file, and always will be in the files I'm editing; however it is repeated later in the text file and these later instances must be kept - is it possible to remove only the first instance of this text?