思路分析:
(推荐教程:Python入门教程)
(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()+"\n"+"radio 2\nchannel\n" f1.writelines(lis)
效果: