I'm making a program which deletes certain lines from an existing file. It takes file1 as entry(f1), it looks for a certain pattern and if it finds it, it modifies the line (to make it compatible with the other file) and saves this modification in a variable 'mark'. It opens another file f2, and searches 'mark' in it. If it finds 'mark' in a certain line in f2, I have to delete that line and the three lines after. The thing is that when I run it, the program deletes everything from f2, so I get an empty file as a result.
new=''
pattern2 = '2:N:0:8'
i=0
f1=open('test_reverse.txt','r')
for line in f1:
if pattern2 in line:
mark=line.replace('2:N:0:8','1:N:0:8')
f2=open('test_OKforward2.txt','r')
lines=f2.readlines()
for i, line in enumerate(lines):
if mark in lines[i]:
e=lines[i]
e1=lines[i+1]
e2=lines[i+2]
e3=lines[i+3]
new=e+e1+e2+e3
f3=open('test_OKforward2.txt','w')
if line!=new:
f3.write(line)
I tried with the next() function as well, but I got the same result and a 'stop iteration' error.