PDA

View Full Version : Saving a list to file



davorp
18-04-2018, 01:30 PM
Hello,

I am writing a small program in python which is a list of my favourite anime. Now, I would like to save the list to file.

This is my code:


myanime=[]
for _ in range(10):
myanime.append(input("Enter a new entry :"))
print('My Favourite anime: \n' , *myanime, sep='\n - ')

This creates a list from the input value. I would now like to save the list to a file, after all the ten entries were added. I have tried with pickle, but it didn’t do what I wanted. How can I save my list to a file? Thank you.

saintomer1866
18-04-2018, 05:27 PM
something like this should work


myanime=[]
for _ in range(10):
newEntry = raw_input("Enter a new entry :")
print "My Favourite anime: \n" + newEntry + "\n - "
myanime.append(newEntry)
f= open("myanime.txt","a+")
for item in myanime:
f.write("%s\n" % item)
f.close()

St.O