[英]How can I write on new lines in a text file while preserving old ones
When I open a text file and write in it and at the end of the program close it, the lines stay in the text file. 当我打开一个文本文件并写入其中,并在程序结束时将其关闭时,这些行将保留在文本文件中。 When i reopen that program, rather than writing on a new set of lines, it overwrites what is already there. 当我重新打开该程序时,而不是在新的一行上编写,它会覆盖已经存在的内容。 I want to keep both pieces of text, it is for data logging. 我想保留这两段文字,这是用于数据记录的。 Any ideas on how to fix this. 有想法该怎么解决这个吗。
You should use the a
mode, like this: 您应该使用a
模式,如下所示:
with open("file","a") as f:
f.write("Something")
This will append to the file instead of overwriting it. 这将追加到文件中而不是覆盖它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.