## Python文件写入
保存数据的最简单的方式之一是将其写入到文件中。通过将输出写入文件,即便关闭包含程序输出的终端窗口,这些输出也依然存在:你可以在程序结束运行后查看这些输出,可与别人分享输出文件,还可编写程序来将这些输出读取到内存中并进行处理。
要写入现有文件,必须向`open()`函数添加参数:
`"a"`\-追加-将追加到文件末尾
`"w"`\-写-将覆盖任何现有内容
示例,打开文件“ demofile2.txt”,并将内容附加到该文件:
```
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
#open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())
```
示例,打开文件“ demofile3.txt”并覆盖内容:
```
f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()
#open and read the file af