小编L0g*_*g1x的帖子

在python中的文件行之间插入文本

我有一个我正在阅读的文件

fo = open("file.txt", "r")
Run Code Online (Sandbox Code Playgroud)

然后通过做

file = open("newfile.txt", "w")
file.write(fo.read())
file.write("Hello at the end of the file")
fo.close()
file.close()
Run Code Online (Sandbox Code Playgroud)

我基本上将文件复制到新文件,但也在新创建的文件末尾添加一些文本.我怎么能插入那条线,在由空行分隔的两条线之间?即:

line 1 is right here
                        <---- I want to insert here
line 3 is right here
Run Code Online (Sandbox Code Playgroud)

我可以通过分隔符\n来标记不同的句子吗?

python io insert tokenize writetofile

2
推荐指数
1
解决办法
2835
查看次数

标签 统计

insert ×1

io ×1

python ×1

tokenize ×1

writetofile ×1