Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple objects in pickle file

Tags:

python

pickle

I want to be able to add multiple objects to pickle file and be able to retrieve it later. I got to the point where I can save objects but when I loading a file, only last entry is loaded. What is my mistake?

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("List.pickle", "wb")
        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 ("List.pickle", "rb") as pickled_file:
        contacts = pickle.load(pickled_file)
else:
    print("Null")

Now it seems like it file is empty when I'm trying to load it:

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)
else:
    print("Null")
like image 476
Joe Doe Avatar asked May 29 '26 21:05

Joe Doe


2 Answers

You are opening your pickle file with mode wb, which truncates (set's the file back to empty before writing anything new). The mode you want is a which opens for appending.

like image 151
Ethan Furman Avatar answered May 31 '26 10:05

Ethan Furman


You are truncating your Pickle file at every iteration of the while loop, since you are opening it in "wb" mode.

Since you are continuously adding contacts to the contacts list during the loop (and then dumping a separate copy of this list with one more contact on each iteration -- probably not what you intend), I would simply take the whole pickle dumping part of your code and move it just after the while loop, dumping the full contacts list once -- no need for multiple objects.

like image 26
Dologan Avatar answered May 31 '26 11:05

Dologan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!