如何在Python中添加到文件末尾?
我有一个制作家庭作业记事本的好主意,我写了这个程序的基础知识。问题是,当我添加更多内容(我重新运行程序)时,所有内容都会被删除并替换为新的家庭作业。我有办法解决这个问题吗如何在Python中添加到文件末尾?,python,python-3.x,Python,Python 3.x,我有一个制作家庭作业记事本的好主意,我写了这个程序的基础知识。问题是,当我添加更多内容(我重新运行程序)时,所有内容都会被删除并替换为新的家庭作业。我有办法解决这个问题吗 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(agend
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+")
致:
您可能应该使用raw_input()并将类型转换为字符串,而不是input() 要在python中附加到文件,请使用“a”属性,因此更改:
agenda=open("agenda.txt","w+")
致:
您可能应该使用raw_input()并将类型转换为字符串,而不是input()