Reputation: 193
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")
Upvotes: 3
Views: 714
Reputation: 4784
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.
Upvotes: 4
Reputation: 6729
Adding to what @Ethan Furman says, there seems to be a difference in the functionality of 'b'
in windows and unix (reference). Your code might work well on unix but would truncate on windows os
Upvotes: 2
Reputation: 69051
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.
Upvotes: 4