Python 3.x 是否有一种方法可以保留关闭程序时所做的更改?

Python 3.x 是否有一种方法可以保留关闭程序时所做的更改?,python-3.x,append,Python 3.x,Append,所以,当我通过.append向列表中添加一个字符串,然后关闭程序窗口时,有没有办法让它实际修改代码? (Python noob,如果我是哑巴,那么很抱歉) 非常感谢根据我使用python的经验,我不这么认为。您可以将列表的内容写入到文件中,然后在重新填充列表后,您附加的任何内容都仍然存在。写入文件的示例如下: #!/usr/bin/python # Open a file in write mode fo = open("foo.txt", "rw+") print "Name of the

所以,当我通过.append向列表中添加一个字符串,然后关闭程序窗口时,有没有办法让它实际修改代码? (Python noob,如果我是哑巴,那么很抱歉)


非常感谢

根据我使用python的经验,我不这么认为。您可以将列表的内容写入到文件中,然后在重新填充列表后,您附加的任何内容都仍然存在。写入文件的示例如下:

#!/usr/bin/python

# Open a file in write mode
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name

# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line

str = "This is 6th line"
# Write a line at the end of the file.
fo.seek(0, 2)
line = fo.write( str )

# Now read complete file from beginning.
fo.seek(0,0)
for index in range(6):
   line = fo.next()
   print "Line No %d - %s" % (index, line)

# Close opend file
fo.close()
此示例来自位于的Python文档