0
所以,當我通過.append向列表中添加一個字符串,然後關閉該程序的窗口,是否有辦法讓它實際改變代碼? (Python的小白,很抱歉,如果我是啞巴)有沒有辦法保持.append在關閉程序時所做的更改?
非常感謝
所以,當我通過.append向列表中添加一個字符串,然後關閉該程序的窗口,是否有辦法讓它實際改變代碼? (Python的小白,很抱歉,如果我是啞巴)有沒有辦法保持.append在關閉程序時所做的更改?
非常感謝
從我與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文檔在http://www.tutorialspoint.com/python/file_write.htm