我在python中創建了一個用於創建/編輯用戶生成列表的程序。我試圖創建一個保存功能,將列表保存到文本文件,但我遇到了問題。它將創建文本文件,但不會將列表的內容寫入文本文件。python,將列表內容寫入文本文件
#!/usr/bin/python
user_list = []
def banner():
logo = open('chlogo2', 'r')
for line in logo:
print line.strip('\n')
logo.close
main(user_list)
def save(user_list):
print "\nENTER A SAVE NAME.\nTHIS WILL BE SAVED AS A .TXT FILE.\n"
w = ("saves/" + raw_input("CNG>SAVE NAME> ") +".txt")
wr = open(w, 'w')
for i in user_list:
i.write(user_list, '\n')
main(user_list)
def prt(user_list):
print '\nCURRENT LIST -\n'
r = user_list
for line in r:
print line.strip('\n')
main(user_list)
def add(user_list):
print "\nTYPE AN ENTRY IN A 'POSITION' then 'ENTRY' FORMAT\nPRESS 'ENTER' WITHOUT AN INPUT TO RETURN TO THE MAIN FUNTION\n"
try:
x = int(raw_input("CNG>ADD>P> "))
y = raw_input("CNG>ADD>E> ")
x -= 1
user_list.insert(x, y)
add(user_list)
except:
main(user_list)
def main(user_list):
option = open('options')
for line in option:
print line.strip('\n')
option.close
opt = raw_input("CHG> ")
if opt == "list":
prt(user_list)
elif opt == 'banner':
banner()
elif opt == 'exit':
print ' '
elif opt == 'empty':
user_list = []
elif opt == 'add':
add(user_list)
elif opt == 'save':
save(user_list)
if __name__ == "__main__":
banner()
爲什麼遞歸? – Copperfield