I have a text files
Text file
>E8|E2|E9D
Football is a good game
Its good for health
you can play it every day
>E8|E2|E10D
Sequence unavailable
>E8|E2|EKB
Cricket
I wrote the following code for detecting sequence unavailable from the text file and write it in a new text file
lastline = None
with open('output.txt', 'w') as W:
with open('input.txt', 'r') as f:
for line in f.readlines():
if not lastline:
lastline = line.rstrip('\n')
continue
if line.rstrip('\n') == 'Sequence unavailable':
_, _, id = lastline.split('|')
data= 'Sequence unavailable|' + id
W.write(data)
W.write('\n')
lastline = None
It work fine , it detect the sequence unavailabe from the text file and write it in a new file , but i want it to delete it from the file which it read from like
input.txt
>E8|E2|E9D
Football is a good game
Its good for health
you can play it every day
>E8|E2|E10D
Sequence unavailable
>E8|E2|EKB
Cricket
input after code should be like this
>E8|E2|E9D
Football is a good game
Its good for health
you can play it every day
>E8|E2|EKB
Cricket