I am creating a registration program (like the fields for Person and Email that you see at the front of notebooks), in order to implement some concepts into a bigger project.
I'm trying to store multiple objects in a .pickle file. I tried the solution at Multiple objects in pickle file but it didn't help. The second time I enter the person to register, it overrides existing data even after I use append function ('a').
Could anyone tell me what I'm doing wrong, or whether there's a better way to do what I'm doing? Here's my code for reference. Thanks in advance.
import pickle
class Person:
def __init__(self, name, email):
self.name = name
self.email = email
def regPerson():
onePersonRegistered = False
if onePersonRegistered == True:
name = input("Person's name: ")
email = input("Person's email: ")
contact = Person(name, email)
with open("contact.pickle", "a") as handle:
pickle.dump(contact.name, handle, protocol=pickle.HIGHEST_PROTOCOL)
pickle.dump(contact.email, handle, protocol=pickle.HIGHEST_PROTOCOL)
with open("contact.pickle", "rb") as handle:
unserialized_data = pickle.load(handle)
print(contact.name and contact.email == unserialized_data)
name = input("Person's name: ")
email = input("Person's email: ")
contact = Person(name, email)
with open("contact.pickle", "wb") as handle:
pickle.dump(contact.name, handle, protocol=pickle.HIGHEST_PROTOCOL)
pickle.dump(contact.email, handle, protocol=pickle.HIGHEST_PROTOCOL)
with open("contact.pickle", "rb") as handle:
unserialized_data = pickle.load(handle)
print(contact.name and contact.email == unserialized_data)
onePersonRegistered == True
regPerson()