如何用换行符添加新句子而不覆盖以前的文本?

时间:2018-12-28 03:13:20

标签: python-3.x

我可以添加新文本,但不能换行

我尝试将换行符添加到hello世界中,但没有添加新行,而只是将我的第一行和hello world添加到了前一行中。

myfile=open("test.txt","a")
myfile.write("\nhello world")
myfile.close()

my first line
hello world

1 个答案:

答案 0 :(得分:0)

这里是一个有关如何基于输入字符串添加新行的示例。

myfile=open('test.txt','a')
myfile.write('Remember,teamwork begins by building trust.\nAnd the only way to do that is to overcome our need for invulnerability.\n-- Patrick Lencioni')
myfile.close()

outputs:

Remember,teamwork begins by building trust.
And the only way to do that is to overcome our need for invulnerability.
-- Patrick Lencioni