I began by creating four functions, for particular tasks that included pickling, unpickling, shelving and unshelving.
Next, I created some try statements to notify the user the point of failure.
After creating the functions, I made a simple I/O section for user interface allowing the user to select any of the 4 functions or 'exit'.
__author__ = 'Andrew Luck' #-------------------------------------------------## Title: Pickle, Unpickle, Shelve, Unshelve# Dev: Andrew Luck# Date: August 2nd, 2015# Desc: Allows users to manage picke binary.# ALuck, 08/17/2015##-------------------------------------------------# #data import pickle, shelve #processing # define 4 functions for pickling, unpickling, shelving, and unshelving # try statements are set for each function with indicators of which function for identification def Picklelist(): try: print("Pickling lists.") variety = ["sweet", "hot", "dill"] #creates 3 lists shape = ["whole", "spear", "chip"] brand = ["Claussen", "Heinz", "Vlassic"] f = open("pickles1.dat", "wb") # sets f to pickles1.dat with wb for "write binary" pickle.dump(variety, f) pickle.dump(shape, f) pickle.dump(brand, f) f.close() except: print('Couldn\'t pickle list.') def unPickelist(): try: print("\nUnpickling lists.") f = open("pickles1.dat", "rb") # reads binary only variety = pickle.load(f) shape = pickle.load(f) brand = pickle.load(f) print(variety) print(shape) print(brand) f.close() except: print('Couldn\'t unpickle list.') def ShelveList(): try: print("\nShelving lists.") s = shelve.open("pickles2.dat") # shelves the pickle object, with default acess mode of c, for reading, writing and creating s["variety"] = ["sweet", "hot", "dill"] s["shape"] = ["whole", "spear", "chip"] s["brand"] = ["Claussen", "Heinz", "Vlassic"] s.sync() # make sure data is written except: print('Couldn\'t unshelve list.') def unShelveList(): try: print("\nRetrieving lists from a shelved file:") print("brand -", s["brand"]) #unshelves via list name print("shape -", s["shape"]) print("variety -", s["variety"]) s.close() input("\n\nPress the enter key to exit.") except: print('Couldn\'t retrieve shelved list.') #input/output #give user a choice menu for running functions while(True): strChoice = input("Enter '1' to pickle the list, '2' to unpickle, '3' to shelve, or '4' to unshelve. To quit, type 'exit'.") if (strChoice == '1'): Picklelist() elif (strChoice == '2'): unPickelist() elif (strChoice == '3'): ShelveList() elif (strChoice == '4'): unShelveList() elif (strChoice == str(strChoice).lower() == 'exit'): break else:print("Please make a valid selection or 'exit' to quit.")