n = open(newfile, 'w')
for i in f:
    if i.find('Banana') == -1:
        n.write(i)
continue
if i.find('\n') == -1:
    i += 'ToothPaste'
else:
    i = i.rstrip('\n')
i += 'ToothPaste\n'
n.write(i)
f.close()
n.close()
os.remove(oldfile)
shutil.move(newfile, oldfile)
python equivalent to sed