Python Open a txt file without clearing everything in it?
1 2 3 4 5 6 7 8 9 10 11 | file = io.open('spam.txt', 'w') file.write(u'Spam and eggs! ') file.close() ....(Somewhere else in the code) file = io.open('spam.txt', 'w') file.write(u'Spam and eggs! ') file.close() |
我想知道如何保存一个可以写入的log.txt文件?我想能够打开一个TXT文件,写它,然后能够打开它以后,并有以前写的内容仍然存在。
对于追加模式,将
1 2 3 4 | file = io.open('spam.txt', 'a') file.write(u'Spam and eggs! ') file.close() |
w(rite)模式截断文件,a(ppend)模式添加到当前内容。
您需要在附加模式下打开它
1 | file = io.open('spam.txt', 'a') |
1 | file = io.open('spam.txt', 'a') |
对append使用模式"a"。