问题描述
|
file = io.open(\'spam.txt\',\'w\')
file.write(u\'Spam and eggs!\\n\')
file.close()
....(Somewhere else in the code)
file = io.open(\'spam.txt\',\'w\')
file.write(u\'Spam and eggs!\\n\')
file.close()
我想知道如何保存可以写入的log.txt文件?
我希望能够打开一个txt文件,对其进行写入,然后可以稍后将其打开,并使先前写入的内容仍然存在。
解决方法
将追加模式从
\'w\'
更改为\'a\'
。但是您真的应该只打开文件并在需要时写入文件。如果要重复,请使用logging
模块。
, file = io.open(\'spam.txt\',\'a\')
file.write(u\'Spam and eggs!\\n\')
file.close()
w(rite)模式将截断文件,a(ppend)模式将添加到当前内容。
, file = io.open(\'spam.txt\',\'a\')
使用模式'a \'进行附加。
, 您需要以附加模式打开它
file = io.open(\'spam.txt\',\'a\')