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.

Thanks.

Recommended Answers

All 10 Replies

You would need to use a file or database. While I rarely use it myself, by pickling/shelving you can store most kinds of python objects (including lists) in files and then retrieve them later.

You can google search python pickling for more details on how to do this.

I was unable to find a tutorial that explained in words and code I understood:confused:.
I tried copying the code of one tutorial but the info I stored was lost after I closed out the window.

...

Simple pickling example:

import cPickle

#store the list
lst = [1, 3, 4, 5, 6, 7, 8]
pickleFile = open("list.dat", "w")
cPickle.dump(pickleFile, lst)
pickleFile.close()

#read back the list
pickleFile = open("list.dat", "r")
lst = cPickle.load(pickleFile)
pickleFile.close()

print lst

Something like that. AFAIK, cPickle works in LIFO mode.

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.

The code Scru gave me does not seem to work, when I run the program it comes up with this error;

Traceback (most recent call last):
File "C:\Python26\pickleExample", line 6, in <module>
cPickle.dump(pickleFile, lst)
TypeError: argument must have 'write' attribute

I was planning to store the cyphers I made with the other program you gave me and call them up (by name or some serial number) whenever I open the program.

cPickle.dump(pickleFile, lst)

help(cPickle.dump)
Help on built-in function dump in module cPickle:

dump(...)
    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)

commented: didn't test that one +6
Member Avatar for leegeorg07

if the other programs have encode/decode programs then you could just import the program and use the functions
e.g.

import mycipher

#if the function doesnt automatically print it
to_encode = 'this is a test'
a = mycipher.encode(to_encode)
print a

#if it does
to_encode = 'this is a test'
mycipher.encode(to_encode)

It works, I switched pickleFile and lst and it now works, even when i make a new program without the code for what lst is. Question, where does it store that information about lst?

And I would like an line-by-line explanation (if it isn't to much trouble) of the code so that I can learn to adapt it.

Thank you.

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
cPickle.dump(lst, pickleFile)
# Close the file.. no more writing
pickleFile.close()

# 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
pickleFile.close()

# Print the results
print lst

Hopefully that helps. If you have any other questions let us know!

Thank you. That is very helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.