Your question already does some stuff to load the contacts. The line loadedcontacts = pickle.load(contacts)
is a good approach. But pickle.load
expects an opened file rather than a file name. So instead of passing contacts
you pass opencontacts
.
You can save the contacts then in a list by creating a list before the outer loop:
allcontacts = [] # Creates an empty list
for root, dirs, files in os.walk("LIP Source Files/Contacts/Contact Book"):
# Omitted
Then you append every contact you unpickle to that list:
loadedcontacts = pickle.load(opencontacts)
allcontacts.append(loadedcontacts)
And just as a side note: You should close the opened file when you don't need it anymore. In this example this means that you call opencontacts.close()
after the call to loadedcontacts = pickle.load(opencontacts)
.