取决于你要做什么。要附加,可以用
"a"打开它:
with open("foo.txt", "a") as f: f.write("new linen")如果要先添加某些内容,则必须先从文件中读取:
with open("foo.txt", "r+") as f: old = f.read() # read everything in the file f.seek(0) # rewind f.write("new linen" + old) # write the new line before


