Explain Codes LogoExplain Codes Logo

Using pickle.dump - TypeError: must be str, not bytes

python
pickle
data-types
binary-mode
Alex KataevbyAlex Kataev·Mar 9, 2025
TLDR

To resolve the TypeError: must be str, not bytes with pickle.dump, open the file in binary mode with 'wb'. Here's the corrected code:

import pickle # byte time, not story time! with open('output.pkl', 'wb') as f: pickle.dump(data_to_save, f)

Replace 'output.pkl' with your filename and data_to_save with the object you're pickling.

Before we pickle

Python's pickle module serializes (pickles) and deserializes (unpickles) Python objects. You've likely run into the "must be str, not bytes" problem, because you were trying to pickle an object into a file opened in text (not binary) mode.

Essentially: you served pickle in a bakery bag, not a pickle jar.

What's in a byte?

Binary mode is important. It keeps everyone happy by making sure data reads and writes are exact. It's the peacekeeper in a land where line ending conversions can ruin the day.

If you're reading from a pickle file, use 'rb' mode:

# Start the byte party! with open('output.pkl', 'rb') as f: data_loaded = pickle.load(f)

Bytes're for serial...ization

If you want to serialize your object into a bytes object before writing it to a file:

# Freeze 'em, Danno serialized_data = pickle.dumps(data_to_save) # Now write out to the file with open('output.pkl', 'wb') as f: f.write(serialized_data)

From bytes back to edible pickles

And if you need to deserialize:

# Re-animation sequence, engage! with open('output.pkl', 'rb') as f: serialized_data = f.read() # Welcome your object back to life data_loaded = pickle.loads(serialized_data)

Tips and tricks

1. Data type matters

Pickling needs bytes. Writing bytes as if they were text... well, it's kind of like giving your cat dog food. They might eat it, but they won't be happy!

2. Protocol can almost rhyme with profit

Protocols - the higher the number, the newer the protocol. Think of it as software years - bigger is younger. But remember your audience—if you're sharing pickled objects with Python 2 users, don't go overboard!

# Use the highest protocol available pickle.dump(data_to_save, f, protocol=pickle.HIGHEST_PROTOCOL)

3. Sharing is caring

When sharing pickles across Python versions, consider the diversity. Protocols from Python 3 can seem like gibberish to Python 2.

4. Safety first!

Unpickling = potential code execution. Remember to only unpickle what you trust.