473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Pickle Stuff and Filename Recognition

23
First of all, the important parts of the code...

Expand|Select|Wrap|Line Numbers
  1. def AddressRW(aw, ar):
  2.     #For Reading & Writing to address file
  3.     while aw == True:
  4.         try:
  5.             #(W)right
  6.             adDict = {}
  7.             adFile = file('address.txt', 'r')
  8.             adDict = Dill.load(adFile)
  9.             adFile = file('address.txt', 'w')
  10.             newName = raw_input("Enter name of resident: ")
  11.             if adDict.has_key(newName):
  12.                 adFile.close
  13.                 ynTest = True
  14.                 while ynTest == True:
  15.                     overChoice = raw_input("Name already in list. Rewright? (y,n)")
  16.                     if overChoice == 'y' or overChoice == 'Y':
  17.                         aw = False
  18.                         ynTest = False
  19.                         AddressAP(True, False, False)
  20.                     elif overChoice == 'n' or overChoice == 'N':
  21.                         aw = False
  22.                         ynTest = False
  23.                     else:
  24.                         print "...Error - Try Again..."
  25.             else:
  26.                 adDict[newName] = raw_input('Enter Address in desired format: ')
and...

Expand|Select|Wrap|Line Numbers
  1. def AddressAP(edit, delete, search):
  2.     #For searching, editing, and deleting address names
  3.     while edit == True:
  4.         try:
  5.             adDict = {}
  6.             adFile = file('address.txt', 'r')
  7.             adDict = Dill.load(adFile)
  8.             adFile = file('address.txt', 'w')
  9.             dictSearch = raw_input("Whose address do you want to edit? ")
  10.             if adDict.has_key(dictSearch):
  11.                 adDict[dictSearch] = raw_input("What is the new address? ")
  12.                 Dill.dump(adDict, adFile)
  13.                 adFile.close
  14.                 edit = False
  15.             else:
  16.                 print "Name not on file."
  17.                 edit = False
  18.         except EOFError:
  19.             print "No addresses on file..."
  20.             edit = False
  21.  

When you run the code and run into a name that's already on file, you get the option to go onto the edit section of the code. When you do this, though, the program always gets the EOFError. I'm guessing the data in the file is deleted (or moved directly to? excuse my ignorance) when it's moved into memory. It's not put back until you leave both functions. I'm not sure how to get around this.


The other problem is that on my school computer, it doesn't recognize 'address.txt' as being there. I keep getting this message:

Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
  3.     exec codeObject in __main__.__dict__
  4.   File "C:\Documents and Settings\student4\Desktop\Python Programs\Addressbook\addressbook.py.py", line 140, in ?
  5.     AddressRW(False, True)
  6.   File "C:\Documents and Settings\student4\Desktop\Python Programs\Addressbook\addressbook.py.py", line 53, in AddressRW
  7.     adFile = file('address.txt', 'r')
  8. IOError: [Errno 2] No such file or directory: 'address.txt'
  9.  
But I think this probably has something to do with the way the cmd prompt is setup...
May 14 '07 #1
3 1708
bvdet
2,851 Expert Mod 2GB
First of all, the important parts of the code...

Expand|Select|Wrap|Line Numbers
  1. def AddressRW(aw, ar):
  2.     #For Reading & Writing to address file
  3.     while aw == True:
  4.         try:
  5.             #(W)right
  6.             adDict = {}
  7.             adFile = file('address.txt', 'r')
  8.             adDict = Dill.load(adFile)
  9.             adFile = file('address.txt', 'w')
  10.             newName = raw_input("Enter name of resident: ")
  11.             if adDict.has_key(newName):
  12.                 adFile.close
  13.                 ynTest = True
  14.                 while ynTest == True:
  15.                     overChoice = raw_input("Name already in list. Rewright? (y,n)")
  16.                     if overChoice == 'y' or overChoice == 'Y':
  17.                         aw = False
  18.                         ynTest = False
  19.                         AddressAP(True, False, False)
  20.                     elif overChoice == 'n' or overChoice == 'N':
  21.                         aw = False
  22.                         ynTest = False
  23.                     else:
  24.                         print "...Error - Try Again..."
  25.             else:
  26.                 adDict[newName] = raw_input('Enter Address in desired format: ')
and...

Expand|Select|Wrap|Line Numbers
  1. def AddressAP(edit, delete, search):
  2.     #For searching, editing, and deleting address names
  3.     while edit == True:
  4.         try:
  5.             adDict = {}
  6.             adFile = file('address.txt', 'r')
  7.             adDict = Dill.load(adFile)
  8.             adFile = file('address.txt', 'w')
  9.             dictSearch = raw_input("Whose address do you want to edit? ")
  10.             if adDict.has_key(dictSearch):
  11.                 adDict[dictSearch] = raw_input("What is the new address? ")
  12.                 Dill.dump(adDict, adFile)
  13.                 adFile.close
  14.                 edit = False
  15.             else:
  16.                 print "Name not on file."
  17.                 edit = False
  18.         except EOFError:
  19.             print "No addresses on file..."
  20.             edit = False
  21.  

When you run the code and run into a name that's already on file, you get the option to go onto the edit section of the code. When you do this, though, the program always gets the EOFError. I'm guessing the data in the file is deleted (or moved directly to? excuse my ignorance) when it's moved into memory. It's not put back until you leave both functions. I'm not sure how to get around this.


The other problem is that on my school computer, it doesn't recognize 'address.txt' as being there. I keep getting this message:

Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
  3.     exec codeObject in __main__.__dict__
  4.   File "C:\Documents and Settings\student4\Desktop\Python Programs\Addressbook\addressbook.py.py", line 140, in ?
  5.     AddressRW(False, True)
  6.   File "C:\Documents and Settings\student4\Desktop\Python Programs\Addressbook\addressbook.py.py", line 53, in AddressRW
  7.     adFile = file('address.txt', 'r')
  8. IOError: [Errno 2] No such file or directory: 'address.txt'
  9.  
But I think this probably has something to do with the way the cmd prompt is setup...
I noticed a few things. Each 'try' must have an 'except' or a 'finally'. You should not use 'try...except' statements until your code is debugged. Some errors can be obfuscated. I assume 'Dill' was defined with 'from pickle import Unpickler as Dill'? This clouds the intent of your code when you ask for help. A pickle file is loaded like this:
Expand|Select|Wrap|Line Numbers
  1. pickle.Unpickler(open(file_name)).load()
Close a file like this:
Expand|Select|Wrap|Line Numbers
  1. f=open(file_name, 'w')
  2. ...do stuff...
  3. f.close()
Notice the '()'. If an error occurs when the file is opened, it may never be written and you will end up with an empty file.

Expand|Select|Wrap|Line Numbers
  1. f = open(file_name, 'w')
  2. # dumps one object
  3. pickle.Pickler(f).dump(object)
  4. f.close()
  5. # loads one object
  6. pickle.Unpickler(open(file_name)).load()
May 14 '07 #2
Bellum
23
Oh, I'm sorry. I'd completely forgotten the Dill thing. It's

import cPickle as Dill

Don't quite understand why I would only import Unpickler as I'm also dumping stuff..?


But yeah, I see what you’re saying. I can't believe I forgot to put the () after .close, and I didn't use Unpickler at all.

Anyway, thanks for the help, I'll probably re-write a bit of it tonight and see how it works.
May 15 '07 #3
Bellum
23
Hmm...

pickle.Pickler(adFile).dump(adDict)

returns a value error. I'm still playing around with it. Didn't have much time to look at everything last night.

About the IOError, I'm thinking it has something to do with the security settings on this computer. Perhaps student users don't have permission to write the right thing, I don't know. Would anyone know what security settings might effect Pythons ability to write to files?
May 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Christian Tismer | last post by:
Martin v. Löwis wrote: > "Mark Hahn" <mark@hahnca.com> writes: > > >>I don't understand how this could happen with pickle. Isn't it supposed to >>stop when it runs into an object it has...
0
by: Jean-Luc | last post by:
Hi I am grafing pickling functionality on a big dictionary of object instances and I am having numerous problems. I think understand the notion of what can/can't be pickled and I know how to...
16
by: Nicolas Fleury | last post by:
Is it possible to use the pickle module to update an object instead of creating a new instance? Thx Nicolas
3
by: Michael Hohn | last post by:
Hi, under python 2.2, the pickle/unpickle sequence incorrectly restores a larger data structure I have. Under Python 2.3, these structures now give an explicit exception from...
0
by: Mike P. | last post by:
Hi all, I'm working on a simulation (can be considered a game) in Python where I want to be able to dump the simulation state to a file and be able to load it up later. I have used the standard...
8
by: Gabriel Genellina | last post by:
Hello I want to convert from pickle format to python source code. That is, given an existing pickle, I want to produce a textual representation which, when evaluated, yields the original object...
6
by: Jim Lewis | last post by:
Pickling an instance of a class, gives "can't pickle instancemethod objects". What does this mean? How do I find the class method creating the problem?
1
by: srinivasan srinivas | last post by:
Hi, I would like to know how to pickle a bound method?? Thanks, Srini Meet people who discuss and share your passions. Go to http://in.promos.yahoo.com/groups/bestofyahoo/
1
by: srinivasan srinivas | last post by:
HI Peter, It works will for instance and class methods. But it doesn't work for static methods. Can you tel me how to pickle static methods as well?? Thanks, Srini ----- Original Message ----...
1
by: IceMan85 | last post by:
Hi to all, I have spent the whole morning trying, with no success to pickle an object that I have created. The error that I get is : Can't pickle 'SRE_Match' object: <_sre.SRE_Match object at...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.