Question

update: I tried making two separate functions for saving and loading the dictionary. Works beautifully. But there still is an issue. The program will not recognize the line emails = updatedEmails

It would unpickle just fine, and those content are in their respective dictionary but I can't get it to assign those to the global 'emails' which all the other functions alter

=====================================================

    import pickle     
    dataList = open('data.txt','wb')
    global emails
    emails = {}

    def loading():
        inFile = open('data.txt', 'rb')
        updatedEmails = pickle.load(inFile)
        print (updatedEmails)
        inFile.close()

    def saving(emails):
        dataList = open('data.txt', 'wb')
        pickle.dump(emails, dataList)
        dataList.close()


    def displayMenu():
        print('\t\t\t\tMenu')
        print('\t5)Display the list')
        print('\t6)Quit')

    def main():
        choice = 0
        loading()
        while choice != 6:
            displayMenu()
            choice = int(input('Enter your choice: '))

            if choice == 1:
                add()
            elif choice == 6:
                saving(emails)
            elif choice == 7:
                loading()
                emails = updatedEmails

    main()
Was it helpful?

Solution

Your code opened the pickle file for writing first:

dataList = open('data.txt','wb')

That truncates the file to 0; by the time you then try to load pickles from that same file it is empty.

Only open the file for writing when you are actually going to write a new pickle to it, not before.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top