Pickle Problem

Gary Herron gherron at islandtraining.com
Thu Mar 15 16:38:34 CET 2007

tonyr1988 wrote:
> I'm a complete python n00b writing my first program (or attempting to,
> anyway). I'm trying to make the transition from Java, so if you could
> help me, it would be greatly appreciated. Here's the code I'm stuck on
> (It's very basic):
> class DemoClass:
> 	def __init__(self):
> 		self.title = ["Hello", "Goodbye"]
> 	def WriteToFile(self, path = "test.txt"):
> 		fw = file(path, "w")
> 		pickle.dump(self.title, fw)
> 		fw.close()
> if __name__=='__main__':
> 	x = DemoClass
> 	x.WriteToFile
> It doesn't do any file I/O at all (that I see). I hope my syntax is
> alright. If I just call WriteToFile, shouldn't it perform with the
> default path? It gives me no errors and pretends to execute just fine.
Several people have pointed out the problem, but when you get that 
fixed, I see another bit of trouble.   The pickle format is a binary 
format (be default), but you don't open the file in binary mode.   On 
Unix the distinction is (wisely) irrelevant, but on Windows you should 
open the file with a mode of "wb" not just "w".

More information about the Python-list mailing list