Hooting
Hooting

Reputation: 1711

python pickle.load works incorrectly on windows platform

when I run the following code

import pickle


class PlayerData:
    def __init__(self, user_name, pwd):
        self.name = user_name
        self.pwd = pwd
        self.pos = 12
        self.online = False
        self.info = {'gunNum': 0,
                     'guns': None,
                     'barrelNum': 0,
                     'barrels': None,
                     'health': 100,
                     'ammos': {'0': {'ammoCurrentCarried': 50,
                                     'ammoName': 'Assault Rifle'},
                                '1': {'ammoCurrentCarried': 30, 'ammoName': 'Assault Rifle RUS'},
                                'number': 2
                               }
                     }


def create_user_file(user):
    file_path = "data/users/" + user.name
    f = open(file_path, "w")
    pickle.dump(user, f, pickle.HIGHEST_PROTOCOL)
    f.close()


def load_user(user_name):
    file_path = "data/users/" + user_name
    f = open(file_path, "r")
    user = pickle.load(f)
    f.close()
    return user


def register(user_name):
        pwd = "ab"
        data = PlayerData(user_name, pwd)
        create_user_file(data)


def login(user_name):
    user = load_user(user_name)
    print user.__dict__

name = "dd"
register(name)
login(name)

the traceback show that:

Traceback (most recent call last):
  File "D:/pycharm/hello/hello.py", line 50, in <module>
    login(name)
  File "D:/pycharm/hello/hello.py", line 45, in login
    user = load_user(user_name)
  File "D:/pycharm/hello/hello.py", line 33, in load_user
    user = pickle.load(f)
  File "C:\Python27\lib\pickle.py", line 1378, in load
    return Unpickler(file).load()
  File "C:\Python27\lib\pickle.py", line 858, in load
    dispatch[key](self)
  File "C:\Python27\lib\pickle.py", line 1169, in load_binput
    i = ord(self.read(1))
TypeError: ord() expected a character, but string of length 0 found

But if I run the previous codes on Mac, it works fine.

Another problem is that, if I drop some attributes of PlayerData, it can work both on windows and mac. For example,

import pickle


class PlayerData:
    def __init__(self, user_name, pwd):
        self.name = user_name
        self.pwd = pwd
        #self.pos = 12 #comment this line
        self.online = False
        self.info = {'gunNum': 0,
                     'guns': None,
                     'barrelNum': 0,
                     'barrels': None,
                     'health': 100,
                     'ammos': {'0': {'ammoCurrentCarried': 50,
                                     'ammoName': 'Assault Rifle'},
                                '1': {'ammoCurrentCarried': 30, 'ammoName': 'Assault Rifle RUS'},
                                'number': 2
                               }
                     }


def create_user_file(user):
    file_path = "data/users/" + user.name
    f = open(file_path, "w")
    pickle.dump(user, f, pickle.HIGHEST_PROTOCOL)
    f.close()


def load_user(user_name):
    file_path = "data/users/" + user_name
    f = open(file_path, "r")
    user = pickle.load(f)
    f.close()
    return user


def register(user_name):
        pwd = "ab"
        data = PlayerData(user_name, pwd)
        create_user_file(data)


def login(user_name):
    user = load_user(user_name)
    print user.__dict__

name = "dd"
register(name)
login(name)

I have no idea how to solve this

Upvotes: 0

Views: 890

Answers (1)

Mike McKerns
Mike McKerns

Reputation: 35207

Try replacing 'r' with 'rb' and 'w' with 'wb'. See this ancient Python bug report: https://mail.python.org/pipermail/python-bugs-list/2005-April/028565.html

Upvotes: 3

Related Questions