I am using python pickle to maintain a contact list. I have 2 questions:
- After a new person is added, I don't see the whole list with all the persons is printed. only the new person is printed. what is wrong with the code?
- If I am going to delete one contact, how can I make the change to local file?
The code:
import pickle
class contact:
person= {};
def add(self, name,contact):
self.person[name] = contact;
store2file(self.person);
#print(self.contactlist);
def delete(self,name):
del self.person[name];
#print(self.person);
def modify(self,name,contact):
self.person[name] = contact;
store2file(self.person);
def store2file(person):
mycontactfile = 'contactlist.data';
f = open(mycontactfile,'wb');
pickle.dump(person,f);
f.close();
f = open(mycontactfile,'rb');
storedcontact = pickle.load(f);
print (storedcontact);
def main():
mycontact = contact();
option = input('Pls select option: 1 Add; 2 delete; 3 update: ');
if option == '1':
name = input('Enter the name: ');
contactNo = input('Enter the contact number: ');
mycontact.add(name,contactNo);
store2file(mycontact);
elif option =='2':
name = input('Enter the name: ');
mycontact.delete(name);
elif option =='3':
name = input('Enter the name: ');
contactNo = input('Enter the contact number: ');
mycontact.modify(name,contactNo);
else:
print('Pls select proper option');
main()