有没有办法在python中追加和读取文本文件?

时间:2018-05-13 09:24:34

标签: text python-3.6

我正在尝试在python 3.6中制作一个使用文本文件保存数据的游戏。

text = open("text_file.txt", "a")
text.write("Line 1")
text.write("Line 2")
text.write("Line 3")
text.close()
text = open("text_file.txt", "r")
print(text.read())
text.close()

有更简单的方法吗? 我知道'r +',但它是读写的组合。问题在于它的写入部分在打开文件时将文本文档重置为空白。

1 个答案:

答案 0 :(得分:1)

来自fopen(3)手册页:

   a+     Open  for  reading  and appending (writing at end of file).  The
          file is created if it does not exist.  The initial file position
          for  reading  is  at  the  beginning  of the file, but output is
          always appended to the end of the file.