I have a file as the format:
xxxxx
yyyyy
zzzzz
ttttt
And I need to write in file between xxxxx and yyyyy lines as:
xxxxx
my
If the file is small then you can simply use str.replace():
>>> !cat abc.txt
xxxxx
yyyyy
zzzzz
ttttt
>>> with open("abc.txt") as f,open("out.txt",'w') as o:
data=f.read()
data=data.replace("xxxxx\nyyyyy","xxxxx\nyourline\nyyyyy")
o.write(data)
....:
>>> !cat out.txt
xxxxx
yourline
yyyyy
zzzzz
ttttt
For a huge file use mgilson's approach.