View Full Version : need help with pythons pickle

March 14th, 2008, 05:32 PM
well im trying to make a simple program that will open a text file, let me edit it, and save it and close it. I was told that pickle was the best way to do this.

however i cant get pickle to work

the error:

Traceback (most recent call last):
File "/home/robert/safe_edit.py", line 14, in <module>
p = pickle.Pickler(in_flile)
NameError: name 'pickle' is not defined

the code:

# Get filepath
filepath = raw_input("Enter file path: ")

# Read file
in_file = open(filepath, "r")
text = in_file.read()

import pickle as p
p = pickle.Pickler(in_flile)

print text

any help or push in the right direction would be greatly appreciated.

March 14th, 2008, 05:45 PM
If you import pickle as p, then the name pickle is not defined. p is defined instead of pickel. You should either remove the "as p" part, or use p instead of pickle in the right side of the assignement instruction, and rename the p variable you are using.

Meanwhile, pickle is a serialization library, useful to persist objects as a file, but not for treating text files.

March 14th, 2008, 05:47 PM
You're doing
import pickle as p So you can't call pickle.Pickler. Call p.Pickler, instead.

March 14th, 2008, 05:53 PM
luisromangz and mssever are right about why you are getting the error message.

But let me question your need for pickle. Why such tricky way? You cannot believe all the advice you get on internet! :-)

Pickle is to store whole object in some magical way so you can restore it later as it was, in single operation. If all you have is a text file, why you need to save it's content pickled? You cannot edit a pickled object. Just save file as text, after some transformation, it is more flexible. IMHO, YMMV.

March 14th, 2008, 06:35 PM
thx guys, that fixed it :p duh

@pmasiar thats what i was thinking and was going to do it that way but could not think of a way to edit the file from within the program. i can read and write but how would i edit?

March 14th, 2008, 07:38 PM
but could not think of a way to edit the file from within the program. i can read and write but how would i edit?

- To edit file in interactive mode: open it in any editor and do it
- to edit file in "programmatic" mode: read it, possibly line by line, or bigger chunks, depending on file structure; then process chunks with your code, and save (write) to another file. When done, rename files.