我有一个针对我的某种操作系统的小代码:
print("Type your document below.")
print("Press enter to save.")
print("Type \\n for a new line.")
file=input()
print("Enter a file name...")
filename=input()
outFile = open(filename, "w+")
outFile.write(file)
outFile.close()
但是当我运行这个代码(在一个 def 中),比如我输入类似于这样的内容:
foo \n bar
因为从用户那里接收输入时,回车键无法起作用,所以你必须使用 \n。
文件的结果是:
foo \n bar
替代:
foo
bar
\n
可以防止实际上创建一个新行,而不是像Type (new line) for a new line
那样。 - Xtreme