我尝试使用以下代码使用 python append 到文件:
with open("test.txt", "a") as myfile:
myfile.write("appended text")
问题是,当我用 vim 打开一个文件时,我在底部收到一条来自 vim 的消息:
"test.txt" [noeol] 2L, 27C
据我了解,这意味着该文件中没有 EOL。它发生在 append python 之后。如果我用 cat 打印文件,我得到:
user@myubuntu:~/py_code$ cat test.txt
appended text
appended textuser@myubuntu:~/py_code$
当我用 vim 编辑 test.txt 并保存时,之后我从 cat 得到结果:
user@myubuntu:~/py_code$ cat test.txt
appended text
appended text
user@myubuntu:~/py_code$
注意“user@myubuntu:~/py_code$”现在应该在新行上。所以我得出结论,在 append python 后 EOL 存在一些问题,但我不明白为什么以及如何解决这个问题。
请您参考如下方法:
将换行符 (\n) 添加到每一行。
myfile.write("appended text\n")
附言。 EOL(行尾)是换行符。
