Joe Doe
Joe Doe

Reputation: 193

Pickle file with many objects

I cannot get my file to store multiple instances of contacts. After adding new contact and trying to print them, it comes up with "IndexError: list index out of range" error. What shall I do to make it work?

import pickle

class People():
    def __init__(self, name, surname, age, mobile_no, home_no):
        self.name = name
        self.surname = surname
        self.age = age
        self.mobile_no = mobile_no
        self.home_no = home_no

    def DisplayContacts(self):
        print("First Name: \t", self.name)
        print("Surname: \t", self.surname)
        print("Age: \t", self.age)
        print("Mobile Number: \t", self.mobile_no)
        print("Home Number: \t", self.home_no)
        print()


def addContact():
    newname = str(input("First name: \t"))
    newsurname = str(input("Surname: \t"))
    newage = int(input("Age: \t"))
    newmobile_no = int(input("Mobile Number: \t"))
    newhome_no = int(input("Home Number: \t"))
    newContact = People(newname, newsurname, newage, newmobile_no, newhome_no) 
    return newContact

cont = 1

contacts = []


while cont == 1:
    user = input("Do you want to add contact? (Y/N)")
    if user == "Y" or user == "y":
        print ("works")
        contacts.append(addContact())
        file = open("CList.pickle", "ab")
        pickle.dump(contacts, file, pickle.HIGHEST_PROTOCOL)
        file.close()
    else:
        print ("111")
        cont = 0


useropen = input("open file? (Y/N)")
if useropen == "Y" or useropen == "y":


    with open ("CList.pickle", "rb") as pickled_file:
        contacts = pickle.load(pickled_file)
        print(contacts[0].surname)
        print(contacts[1].surname)


else:
    print("Null") 

Upvotes: 0

Views: 273

Answers (1)

tdelaney
tdelaney

Reputation: 77347

Simply appending a picked object to a file is not the same thing as pickling a list. EAch time you append, you've created another pickled record. Read the file multiple times to get your list:

with open ("CList.pickle", "rb") as pickled_file:
    contacts = []
    try:
        while True:
            contacts.append(pickle.load(pickled_file))
    except EOFError:
            pass

Now, instead of appending the list of contacts (which would give you a list of lists with many duplicates), just pickle the new contact:

with open("CList.pickle", "ab") as _file:
    while True:
        user = input("Do you want to add contact? (Y/N)")
        if user == "Y" or user == "y":
            print ("works")
            pickle.dump(addContact(), _file, pickle.HIGHEST_PROTOCOL)
        else:
            print ("111")
            break

Upvotes: 1

Related Questions