0

I was wondering how I can save a list entered by the user. I was wondering how to save that to a file. When I run the program it says that I have to use a string to write to it. So, is there a way to assign a list to a file, or even better every time the program is run it automatically updates the list on the file? That would be great the file would ideally be a .txt.

stuffToDo = "Stuff To Do.txt"
WRITE = "a"
dayToDaylist = []
show = input("would you like to view the list yes or no")
if show == "yes":
    print(dayToDaylist)

add = input("would you like to add anything to the list yes or no")
if add == "yes":
    amount=int(input("how much stuff would you like to add"))
    for number in range (amount):
        stuff=input("what item would you like to add 1 at a time")
        dayToDaylist.append(stuff)
remove = input("would you like to remove anything to the list yes or no")
    if add == "yes":        
    amountRemoved=int(input("how much stuff would you like to remove"))
    for numberremoved in range (amountRemoved):
        stuffremoved=input("what item would you like to add 1 at a time")
        dayToDaylist.remove(stuffremoved);
print(dayToDaylist)

file = open(stuffToDo,mode = WRITE)
file.write(dayToDaylist)
file.close()
Paolo
  • 20,112
  • 21
  • 72
  • 113
cizwiz
  • 5
  • 1
  • 9
  • 1
    you can pickle the list – Padraic Cunningham Nov 15 '14 at 22:54
  • By the way, it's generally MORE readable to "hard-code" the mode parameter to open(), not less. Using that pseudo-constant is just confusing. Also, you don't need to give it as a keyword argument; you can just call it as `open("filename", "a")` – Schilcote Nov 15 '14 at 23:19

3 Answers3

5

You can pickle the list:

import pickle

with open(my_file, 'wb') as f:
    pickle.dump(dayToDaylist, f)

To load the list from the file:

with open(my_file, 'rb') as f:
    dayToDaylist = pickle.load( f)

If you want to check if you have already pickled to file:

import pickle
import os
if os.path.isfile("my_file.txt"): # if file exists we have already pickled a list
    with open("my_file.txt", 'rb') as f:
        dayToDaylist = pickle.load(f)
else:
    dayToDaylist  = []

Then at the end of your code pickle the list for the first time or else update:

with open("my_file.txt", 'wb') as f:
    pickle.dump(l, f) 

If you want to see the contents of the list inside the file:

import ast
import os
if os.path.isfile("my_file.txt"):
    with open("my_file.txt", 'r') as f:
        dayToDaylist = ast.literal_eval(f.read())
        print(dayToDaylist)

with open("my_file.txt", 'w') as f:
    f.write(str(l))
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

for item in list: file.write(item)

You should check out this post for more info: Writing a list to a file with Python

Community
  • 1
  • 1
Prasad Shinde
  • 652
  • 2
  • 8
  • 21
0

Padraic's answer will work, and is a great general solution to the problem of storing the state of a Python object on disk, but in this specific case Pickle is a bit overkill, not to mention the fact that you might want this file to be human-readable.

In that case, you may want to dump it to disk like such (this is from memory, so there may be syntax errors):

with open("list.txt","wt") as file:
    for thestring in mylist:
        print(thestring, file=file)

This will give you a file with your strings each on a separate line, just like if you printed them to the screen.

The "with" statement just makes sure the file is closed appropriately when you're done with it. The file keyword param to print() just makes the print statement sort of "pretend" that the object you gave it is sys.stdout; this works with a variety of things, such as in this case file handles.

Now, if you want to read it back in, you might do something like this:

with open("list.txt","rt") as file:
    #This grabs the entire file as a string
    filestr=file.read()
mylist=filestr.split("\n")

That'll give you back your original list. str.split chops up the string it's being called on so that you get a list of sub-strings of the original, splitting it every time it sees the character you pass in as a parameter.

Schilcote
  • 2,344
  • 1
  • 17
  • 35