I saw this code on stack overflow

def writeProfile(profileName, profileData):
    """ pickle user profile data """

    os.chdir(PROFILE_DIR)

    fileName = profileName + ".dat"
    f = open(fileName, "wb")
    pickle.dump(profileData, f)
    f.close()

    os.chdir("..")

It is for saving progress under the pickle module. I don't see a way to save variables. How would I do that?

Well, pickle.dump() should have no problem saving a variable, as in pickle.dump(variable,f). Now if pickle doesn't like that it will raise an exception, but I suspect if you just try it will work fine...based on pickle.dumps() working on a test case I just tried.

You will probably find your real problem comes later, when tryng to unpickle. But that's later...

I just stumbled into this thread and, not being a python programmer, found it quite aumusing ... pickle, unpickle, huh? Can you cucumber.dump() too?

@dani
pickle is for turning objects*
into text,to write to a file so you
can save variables

*Most objects, including user-defined, If not, you can customize to do it.
It can even do recusive lists!

Edited 3 Years Ago by james.lu.75491856

My preference would be Python module shelve, which creates a 'persistent to file' dictionary. Dictionary objects are highly optimized in Python.

My preference would be Python module shelve, which creates a 'persistent to file' dictionary.

I don't like shelve. I think it is very slow. Did you try cerealizer ? (in linux mint, install it with the package manager!)

Edited 3 Years Ago by Gribouillis

This question has already been answered. Start a new discussion instead.