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
Here I am not using file.readlines method, as it fetches all the lines from the file into a list. So, it is not memory efficient.
import os
with open('input.txt') as f1, open('output.txt', 'w') as f2,\
open('temp_file','w') as f3:
lines = [] # store lines between two `>` in this list
for line in f1:
if line.startswith('>'):
if lines:
f3.writelines(lines)
lines = [line]
else:
lines.append(line)
elif line.rstrip('\n') == 'Sequence unavailable':
f2.writelines(lines + [line])
lines = []
else:
lines.append(line)
f3.writelines(lines)
os.remove('input.txt')
os.rename('temp_file', 'input.txt')
Demo:
$ cat 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
$ python so.py
$ cat input.txt
>E8|E2|E9D
Football is a good game
Its good for health
you can play it every day
>E8|E2|EKB
Cricket
$ cat output.txt
>E8|E2|E10D
Sequence unavailable
For generating the temp file you can also use the tempfile module.
No need of temp file with this method:
import fileinput
with open('output.txt', 'w') as f2:
lines = []
for line in fileinput.input('input.txt', inplace = True):
if line.startswith('>'):
if lines:
print "".join(lines),
lines = [line]
else:
lines.append(line)
elif line.rstrip('\n') == 'Sequence unavailable':
f2.writelines(lines + [line])
lines = []
else:
lines.append(line)
with open('input.txt','a') as f:
f.writelines(lines)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With