Python:写入新文件行 [英] Python: Write to new file line
本文介绍了Python:写入新文件行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在四处看看是否可以找到一种方法,以使每次用户输入时都可以在文件中写入新行.基本代码是这样的:
I have been looking around to see if I can find a way to write to a new line in a file every time the user inputs. The basic code is this:
while True:
f = open(server,"w")
initchat = str(input("Chat: "))
chat = str((user + " - " + initchat))
f.write(chat)
f.write("\n")
f.close
许多答案是在字符串中添加\ n,但这仅在之后添加新行,并且不允许将新行写入.我有道理吗?任何帮助将不胜感激谢谢
Many of the answers have been to add \n to the string but that only adds a new line after and doesn't allow the new line to be written to. Do I make any sense? Any help would be much appreciated Thanks
推荐答案
问题是这样的:
f = open(server,"w")
上面只写了1行:
您必须使用"a"
进行追加.
f = open(server,"a")
顺便说一句:您还必须在while:
BTW: you also have to indent your code after while:
这篇关于Python:写入新文件行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文