Python Pickle issue

Merry sunday people. I’ve been trying to study Pickles today in Python and I had this small strip of code which I’m having a slight problem with. I’m using Ubuntu and Blender 2.62.

I keep getting error: line 16
scores = pickle.load(f)
“io.UnsupportedOperation: read”


def loadscores():
    import bge
    import pickle
    import os
    
    logic = bge.logic
    filename = "scores.save"
    file = os.path.join(bge.logic.expandPath("//assets/python/"),filename)

    name = "Matti"
    score = 3400

    data = [name, score]
    with open(file, 'wb') as f:
        pickle.dump(data, f)
        scores = pickle.load(f)
    print(scores)

I personally wouldn’t use pickles. They can be relatively slow. Since I don’t think what your doing is too complicated, I’d just have Python parse to a txt file.

But wouldn’t it be silly to leave a highscore table out in the open as a txt file. Someone could just slap overly huge highscores and a name and save the file?

The differences are negligible, if any.

The pickle module should be used whenever possible.

That can be done just as easily with a pickled file.

Anyway, you’re trying to load from a file handle in write mode, which is why you’re getting the error.

Hah doh, should have noticed but hadn’t had my cup of coffee yet, thank you for an answer :slight_smile:
Is it really as easy to “manipulate” scores stored as binary data in pickles? I mean of course I can’t prevent it fully with these methods, but just trying to make it less attractive to a common gamer to do so.

If someone really wants to cheat, they’ll find a way.

I can just fire up a python interpreter, load the pickled data structure, modify it, and then store it back to the file - I don’t have to do anything from within the game.

Actually, even if you open ‘scores.save’ in a text editor, you should see recognizable patterns (strings will show up as strings, even in a binary file). It doesn’t take much to figure things out from there.