UnpicklingError: pickle data was truncated when trying to read a dictionary from a shelved file

12,971

I just had the same problem, and after a little investigation I realized it probably happened because I stopped my program like a jerk (terminated it in the middle of using the shelve).

So I deleted my shelve and created it again and everything worked fine.

I assume you had the same error, maybe you exited your infinite while loops by terminating the program or something?

Share:
12,971

Related videos on Youtube

J. Browning
Author by

J. Browning

Updated on September 16, 2022

Comments

  • J. Browning
    J. Browning over 1 year

    I'm a teacher, and I'm trying to write a simple function that saves my students' emails in a dictionary for use in another program. I need the dictionary to be saved across multiple executions, so I'm trying to use shelve to save it; however, after running the function for a second time, I get an unpickling error saying the pickle data was truncated. Here is the code:

    shelfFile = shelve.open('mydata')
    studentEmails = shelfFile['studentEmails']
    def inputEmails():
        while True:
            nameInput = input('Name: ')
            if nameInput == '':
                break
            emailInput = input('Email: ')
            if emailInput == '':
                print('Email not entered. Please try again.')
                continue
            while True:
                print('Is this information correct? [Y]es or [N]o')
                print('Name: ' + nameInput)
                print('Email: ' + emailInput)
                correctChoice = input('[Y] or [N]: ').upper()
                if correctChoice == 'Y':
                    studentEmails[nameInput] = emailInput
                    break
                elif correctChoice == 'N':
                    print('Okay. Please input again.')
                    break
                else:
                    print('I did not understand that response.')
    inputEmails()
    shelfFile['studentEmails']=studentEmails
    shelfFile.close()
    

    I create the empty dictionary shelfFile['studentEmails'] in the shell before I run the program. It will run fine the first time, but give me the _pickle.UnpicklingError: pickle data was truncated error when I try to assign the shelfFile back to studentEmails. I'm new at this and still learning, so I appreciate the help.

  • sɐunıɔןɐqɐp
    sɐunıɔןɐqɐp almost 6 years
    (This post does not seem to provide a quality answer to the question. Please either edit your answer, or just post it as a comment to the question).
  • HUCK45
    HUCK45 almost 6 years
    It provides an explanation to his error message and a way to solve it, I would love receiving such answers. Especially if my question was asked about an year ago and I already found a workaround. I tried commenting but sadly this site doesn't allow it for new comers ("You must have 50 reputation to comment").
  • iMath
    iMath over 5 years
    whenever there is an update to the Shelf, I followed with a Shelf.sync() , and I succeed to get rid of the issue