我有一个关于家庭作业记事本的好主意,并且我编写了该程序的基础知识。问题是,当我添加更多内容(重新运行程序)时,所有内容都会被清除并替换为新的作业。有什么办法可以解决这个问题吗?
def query_period(agenda_file):
per = input("What period is it?")
if per in ["1", "2", "3", "4", "5", "6", "7", "8"]:
input_and_write_homework(agenda_file)
else:
print("Invalid choice")
def input_and_write_homework(file):
hw = raw_input("What is the homework?")
file.write(hw)
if __name__ == "__main__":
agenda = open("agenda.txt", "w+")
query_period(agenda)
agenda.close()
最佳答案
要在 python 中附加到文件,请使用“a”属性,因此请更改:
agenda=open("agenda.txt","w+")
至:
agenda=open("agenda.txt","a")
您可能应该使用 raw_input() 并将类型转换为字符串,而不是 input()。
关于python - 如何在 Python 中添加到文件末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8860288/