Pickle Stuff and Filename Recognition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bellum
    New Member
    • Sep 2006
    • 23

    #1

    Pickle Stuff and Filename Recognition

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

    Code:
    def AddressRW(aw, ar):
        #For Reading & Writing to address file
        while aw == True:
            try:
                #(W)right
                adDict = {}
                adFile = file('address.txt', 'r')
                adDict = Dill.load(adFile)
                adFile = file('address.txt', 'w')
                newName = raw_input("Enter name of resident: ")
                if adDict.has_key(newName):
                    adFile.close
                    ynTest = True
                    while ynTest == True:
                        overChoice = raw_input("Name already in list. Rewright? (y,n)")
                        if overChoice == 'y' or overChoice == 'Y':
                            aw = False
                            ynTest = False
                            AddressAP(True, False, False)
                        elif overChoice == 'n' or overChoice == 'N':
                            aw = False
                            ynTest = False
                        else:
                            print "...Error - Try Again..."
                else:
                    adDict[newName] = raw_input('Enter Address in desired format: ')
    and...

    Code:
    def AddressAP(edit, delete, search):
        #For searching, editing, and deleting address names
        while edit == True:
            try:
                adDict = {}
                adFile = file('address.txt', 'r')
                adDict = Dill.load(adFile)
                adFile = file('address.txt', 'w')
                dictSearch = raw_input("Whose address do you want to edit? ")
                if adDict.has_key(dictSearch):
                    adDict[dictSearch] = raw_input("What is the new address? ")
                    Dill.dump(adDict, adFile)
                    adFile.close
                    edit = False
                else:
                    print "Name not on file."
                    edit = False
            except EOFError:
                print "No addresses on file..."
                edit = False

    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:

    Code:
    Traceback (most recent call last):
      File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
        exec codeObject in __main__.__dict__
      File "C:\Documents and Settings\student4\Desktop\Python Programs\Addressbook\addressbook.py.py", line 140, in ?
        AddressRW(False, True)
      File "C:\Documents and Settings\student4\Desktop\Python Programs\Addressbook\addressbook.py.py", line 53, in AddressRW
        adFile = file('address.txt', 'r')
    IOError: [Errno 2] No such file or directory: 'address.txt'
    But I think this probably has something to do with the way the cmd prompt is setup...
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Bellum
    First of all, the important parts of the code...

    Code:
    def AddressRW(aw, ar):
        #For Reading & Writing to address file
        while aw == True:
            try:
                #(W)right
                adDict = {}
                adFile = file('address.txt', 'r')
                adDict = Dill.load(adFile)
                adFile = file('address.txt', 'w')
                newName = raw_input("Enter name of resident: ")
                if adDict.has_key(newName):
                    adFile.close
                    ynTest = True
                    while ynTest == True:
                        overChoice = raw_input("Name already in list. Rewright? (y,n)")
                        if overChoice == 'y' or overChoice == 'Y':
                            aw = False
                            ynTest = False
                            AddressAP(True, False, False)
                        elif overChoice == 'n' or overChoice == 'N':
                            aw = False
                            ynTest = False
                        else:
                            print "...Error - Try Again..."
                else:
                    adDict[newName] = raw_input('Enter Address in desired format: ')
    and...

    Code:
    def AddressAP(edit, delete, search):
        #For searching, editing, and deleting address names
        while edit == True:
            try:
                adDict = {}
                adFile = file('address.txt', 'r')
                adDict = Dill.load(adFile)
                adFile = file('address.txt', 'w')
                dictSearch = raw_input("Whose address do you want to edit? ")
                if adDict.has_key(dictSearch):
                    adDict[dictSearch] = raw_input("What is the new address? ")
                    Dill.dump(adDict, adFile)
                    adFile.close
                    edit = False
                else:
                    print "Name not on file."
                    edit = False
            except EOFError:
                print "No addresses on file..."
                edit = False

    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:

    Code:
    Traceback (most recent call last):
      File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
        exec codeObject in __main__.__dict__
      File "C:\Documents and Settings\student4\Desktop\Python Programs\Addressbook\addressbook.py.py", line 140, in ?
        AddressRW(False, True)
      File "C:\Documents and Settings\student4\Desktop\Python Programs\Addressbook\addressbook.py.py", line 53, in AddressRW
        adFile = file('address.txt', 'r')
    IOError: [Errno 2] No such file or directory: 'address.txt'
    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:
    Code:
    pickle.Unpickler(open(file_name)).load()
    Close a file like this:
    Code:
    f=open(file_name, 'w')
    ...do stuff...
    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.

    Code:
    f = open(file_name, 'w')
    # dumps one object
    pickle.Pickler(f).dump(object)
    f.close()
    # loads one object
    pickle.Unpickler(open(file_name)).load()

    Comment

    • Bellum
      New Member
      • Sep 2006
      • 23

      #3
      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.

      Comment

      • Bellum
        New Member
        • Sep 2006
        • 23

        #4
        Hmm...

        pickle.Pickler( adFile).dump(ad Dict)

        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?

        Comment

        Working...