1

Hello I have an Error and I don´t the reason:

>>> class Fruits:pass
...
>>> banana = Fruits()
>>> banana.color = 'yellow'
>>> banana.value = 30
>>> import pickle
>>> filehandler = open("Fruits.obj",'w')
>>> pickle.dump(banana,filehandler)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python31\lib\pickle.py", line 1354, in dump
Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
TypeError: must be str, not bytes
>>>

I don´t know how to solve this error because I don´t understand it. Thank you so much.

4
  • works for me. Was the namespace somehow polluted before this? Commented Dec 25, 2010 at 13:03
  • @Andrew Jaffe:Sorry...I don´t understand you...what do you mean? Commented Dec 25, 2010 at 13:11
  • Does it work if these are the very first things that you do in a python session? Commented Dec 25, 2010 at 13:26
  • I corrected it based on the answer below and now it works, but I don´t still how to load my objects. Commented Dec 25, 2010 at 14:29

1 Answer 1

3

You have to open your filehandler in binary mode, use wb instead of w:

filehandler = open(b"fruits.obj","wb")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.