Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中添加到文件末尾?_Python_Python 3.x - Fatal编程技术网

如何在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()