Cannot load pickled object
Solution 1:
You need to either read the file first (as binary bytes
) and use pickle.loads()
, or pass an open file object to the pickle.load()
command. The latter is preferable:
with open('out/cache/' +hashed_url, 'rb') as pickle_file:
content = pickle.load(pickle_file)
Neither method supports loading a pickle from a filename.
Solution 2:
If you happen to be porting python2 to 3 and run into this error, python2 and 3 handle bytes different leading to the requirement to open your file handle with the 'b' option. For instance in python2 open(file, 'r') as f: my_list = pickle.load(f)
works , but not in python3. Instead you must open with open(file, 'rb') as f: my_list = pickle.load(f)