需求:
分析:
1)打开文件
2)readlines() 读文件(按行读) --> 拿到文件内容 data
3)循环遍历 data 的每行,在每行后面进行拼接要插入的内容
4)往新文件里写
代码示例:
with open("aa.txt", "w", encoding="utf-8") as f1: with open("ap.txt", "r+", encoding="utf-8") as f: data = f.readlines() for line in data: lis = line.strip()+" "+"radio 2 channel " f1.writelines(lis)
完成插入: