如何追加到文件而不是覆盖它?


当前回答

我总是这样做,

f = open('filename.txt', 'a')
f.write("stuff")
f.close()

它很简单,但非常有用。

其他回答

我总是这样做,

f = open('filename.txt', 'a')
f.write("stuff")
f.close()

它很简单,但非常有用。

您也可以在r+模式下打开文件,然后将文件位置设置为文件末尾。

import os

with open('text.txt', 'r+') as f:
    f.seek(0, os.SEEK_END)
    f.write("text to add")

以r+模式打开文件将允许您写入除结尾之外的其他文件位置,而a和a+强制写入结尾。

您需要以追加模式打开文件,将“a”或“ab”设置为模式。请参见open()。

当您以“a”模式打开时,写入位置将始终位于文件末尾(追加)。您可以用“a+”打开以允许读取、向后搜索和读取(但所有写入仍将位于文件末尾!)。

例子:

>>> with open('test1','wb') as f:
        f.write('test')
>>> with open('test1','ab') as f:
        f.write('koko')
>>> with open('test1','rb') as f:
        f.read()
'testkoko'

注意:使用“a”与使用“w”打开并查找文件结尾不同-请考虑如果另一个程序打开文件并在查找和写入之间开始写入,可能会发生什么情况。在某些操作系统上,使用“a”打开文件可以保证后续的所有写入都会自动附加到文件末尾(即使文件因其他写入而增长)。


关于“A”模式如何运行的更多细节(仅在Linux上测试)。即使向后搜索,每次写入都会附加到文件末尾:

>>> f = open('test','a+') # Not using 'with' just to simplify the example REPL session
>>> f.write('hi')
>>> f.seek(0)
>>> f.read()
'hi'
>>> f.seek(0)
>>> f.write('bye') # Will still append despite the seek(0)!
>>> f.seek(0)
>>> f.read()
'hibye'

事实上,fopen手册页指出:

以追加模式打开文件(a作为模式的第一个字符)导致此流的所有后续写入操作发生在文件结尾,就像在调用之前一样:fseek(流,0,SEEK_END);


旧的简化答案(不与一起使用):

示例:(在实际程序中,使用关闭文件-请参阅文档)

>>> open("test","wb").write("test")
>>> open("test","a+b").write("koko")
>>> open("test","rb").read()
'testkoko'

将open()中的模式设置为“a”(追加)而不是“w”(写入):

with open("test.txt", "a") as myfile:
    myfile.write("appended text")

文档列出了所有可用模式。

Python有三种主要模式的多种变体,这三种模式是:

'w'   write text
'r'   read text
'a'   append text

因此,要附加到文件,很简单:

f = open('filename.txt', 'a') 
f.write('whatever you want to write here (in append mode) here.')

还有一些模式只会使代码行数更少:

'r+'  read + write text
'w+'  read + write text
'a+'  append + read text

最后,有两种二进制格式的读/写模式:

'rb'  read binary
'wb'  write binary
'ab'  append binary
'rb+' read + write binary
'wb+' read + write binary
'ab+' append + read binary