在Windows中编写我的python 3.3程序,我遇到了一个小问题。我正在试图写一些指令到一个文件,以便程序执行。但是,每次我file.write()下一行时,它会替换上一行。我希望能够尽可能多地写入这个文件。注意:使用“\ n”似乎不起作用,因为您不知道将会有多少行。请帮忙!这里是我的代码(即一个循环,我不运行这个多次):Python如何继续写入文件而不擦除已存在的内容
menu = 0
while menu != None:
menu = lipgui.choicebox("Select an option:", choices=["choice1", "choice2", "choice3"])
file = open("file.txt", "w")
if menu == "choice1":
text_to_write = lipgui.enterbox("Text to write:")
file.write(text_to_write)
也因为它是python 3.3,你应该使用open('file.txt','w')作为文件而不是直接打开 – user3012759 2014-08-28 16:17:21
@ user3012759该功能是在Python 2.5中引入的 – Tshepang 2014-09-06 04:32:46