By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,400 Members | 903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,400 IT Pros & Developers. It's quick & easy.

Pickle Stuff and Filename Recognition

P: 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
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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

P: 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

P: 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

Post your reply

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