with open(tmptxt, 'w') as outfile:
    with open(txt, 'r') as infile:
        rowIter = iter(infile)
for row in rowIter:
    if row.startswith('foo2'):
        break
print(row.rstrip(), repr(row))
print('foo bar')
print(row)
for row in rowIter:
    pass
Вставка строки в указанной позиции текстового файла