How do you store info (such as highscores, phone numbers, etc.) for your python program?
I am thinking like you enter things into a list and then the next time you open the program you can acces that list with all the old info.
If you are using a normal flat file instead of pickling, you can only store strings. While you can use writelines for writing a list, I would suggest that you write to the file in a format that you understand, so you can read it back into a program as well. If you present some code for what you are trying to do, then perhaps we can be more specific.
Help on built-in function dump in module cPickle:
dump(obj, file, protocol=0) -- Write an object in pickle format to the given file.
See the Pickler docstring for the meaning of optional argument proto.
Looks like the order's backwards... try instead: cPickle.dump(lst, pickleFile)
The info is stored in the file that is specified in this case it's stored in pickleFile , which points to the file list.dat . Here is scru's code reposted with comments explaining each line:
import cPickle # import module for pickling
# create the list
lst = [1, 3, 4, 5, 6, 7, 8]
# now we open a file, ( 'w' for write mode )
# If the file doesn't exist it is created
# If the file DOES exist, it will be wiped
# using 'a' instead of 'w' will preserve contents
# a = append, w = write, r = read (default)
pickleFile = open("list.dat", "w")
# Dump the object lst to the file pointed to by pickleFile
# Close the file.. no more writing
# Now open the file again... this time for reading
pickleFile = open("list.dat", "r")
# load the contents of pickleFile into the variable lst
lst = cPickle.load(pickleFile)
# Close the file.. no more reading
# Print the results
Hopefully that helps. If you have any other questions let us know!