Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-05-2011, 10:29 PM   #1
Registered: Nov 2004
Location: SW Missouri, USA
Distribution: Debian 6 "Squeeze"
Posts: 55

Rep: Reputation: 7
python3 - trying to pickle object raises TypeError

I'm not sure where I'm going wrong.

import pickle
class theClass:
	attribute1 = None
	attribute2 = None
	attribute3 = None

instance = theClass()
instance.attribute1 = 'hello'
instance.attribute2 = 'hi'
instance.attribute3 = 'boo'

	file = open('pickle', 'w')
	pickle.dump(instance, file)
except IOError as complaint:
Traceback (most recent call last):
  File "", line 14, in <module>
    pickle.dump(instance, file)
  File "/usr/lib/python3.1/", line 1345, in dump
    Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
TypeError: must be str, not bytes
I'm using python3. What am I doing wrong?
Old 04-06-2011, 03:38 AM   #2
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Well I am not 100% sure (and do not have P3 installed on this machine to test), but in the doco for pickle.dump is:

The file argument must have a write() method that accepts a single bytes argument. It can thus be an on-disk file opened for binary writing, a io.BytesIO instance, or any other custom object that meets this interface.

Whereas you have opened the file simply for writing, so maybe it needs to be:
file = open('pickle', 'wb')
Have you also tried to just write a simple string and then build to the class?
Old 04-06-2011, 07:37 AM   #3
Registered: Nov 2004
Location: SW Missouri, USA
Distribution: Debian 6 "Squeeze"
Posts: 55

Original Poster
Rep: Reputation: 7
thanks! it worked!

After seeing the tidbit about the doc, I decided to look again. It mentions a couple times that pickle generates a byte stream. I didn't see where it tells you that the file must be a binary-mode file. However, way down at the very bottom, there is a printed example that shows that the fh is indeed 'wb'.

It reminds me of the "some features may vary on your model" statement in micro-print on the manual of modern microwaves.

Thank you again for your time!


error, python

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
TypeError: list indices must be integers felix001 Programming 1 12-27-2010 08:52 AM
python pickle dump and load jabfinger Programming 5 02-26-2009 07:02 AM
TypeError: rpmdb open failed Red Knuckles Fedora 8 11-05-2006 01:40 PM
A real lilo pickle legacyboy Mandriva 6 06-05-2006 02:25 PM
Resolution Pickle! Please Help! Geepak Linux - Newbie 4 02-08-2005 09:20 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:42 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration