Python save list and read data from file
<p>Basically I would like to save a list to python and then when the program starts I would like to retrieve the data from the file and put it back into the list.
<br> 
So far this is the code I am using</p>

<pre><code>mylist = pickle.load("save.txt")
...
saveToList = (name, data)
mylist.append(saveList)
import pickle
pickle.dump(mylist, "save.txt")
</code></pre>

<p>But it just returns the following error: TypeError: file must have 'read' and 'readline' attributes</p>

<p>You need a file object, not just a file name. Try this for saving:</p>

<pre><code>pickle.dump(mylist, open("save.txt", "wb"))
</code></pre>

<p>or better, to guarantee the file is closed properly:</p>

<pre><code>with open("save.txt", "wb") as f:
    pickle.dump(mylist, f)
</code></pre>

<p>and then this for loading:</p>

<pre><code>with open("save.txt", "rb") as f:
    mylist = pickle.load(f)
</code></pre>

<p>Also, I suggest a different extension from <code>.txt</code>, like maybe <code>.dat</code>, because the contents is not plain text.</p>

python