python 文本特定某一字符串后添加一行字符串

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
file = open(‘a’,'r')
content = file.read()
post = content.find(keyword)
if post != -1:
    content = content[:post+len(keyword)]+str+content[post+len(keyword):]
    file = open(‘a’,'w')
    file.write(content)
file.close()
txt = './out.txt'
aim = '点击量'
addon = 'goal\n'

with open(txt, 'r', encoding='utf-8') as f:
    s = f.readlines()

with open(txt, 'w', encoding='utf-8') as f:
    for i in s:
        f.writelines(i)
        if aim in i:
            f.writelines(addon)