Repository navigation
Expand file tree
/
Copy pathfunctions.py
More file actions
executable file
·80 lines (73 loc) · 2.49 KB
/
Copy pathfunctions.py
File metadata and controls
executable file
·80 lines (73 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import pickle, os
class Contacts:
fileName = 'AddressBook'
######################################################
## 1. Add contacts to a list, nothing special again ####
######################################################
def add(self, name, phone):
if len(phone) >= 10:
contacts = {}
f = open(Contacts.fileName,'rb')
contacts = pickle.load(f)
f.close()
f = open(Contacts.fileName,'wb')
contacts [name] = phone
pickle.dump(contacts, f)
print ("Contact with name {0} and number {1} has been added to the contact list".format(name,phone))
else:
print("The phone number you entered is too short. Please enter a valid number")
######################################################
## 2. Just a regular function to display #########
## data from a variable #########
######################################################
def show_contacts(self):
try:
f = open(Contacts.fileName,'rb')
contacts = pickle.load(f)
if len(contacts) == 0:
print ("No contacts in the contact list as of now")
else:
print("\n")
for name,number in contacts.items():
print ("Name: {0} \t Number: {1}".format(name,number))
print("\n")
f.close()
except EOFError:
print ("There are no contacts in your contact list")
######################################################
## 3. Delete contacts by name #########
######################################################
def delete(self, name):
try:
f = open(Contacts.fileName, 'rb')
contacts = pickle.load(f)
f.close()
del contacts[name]
f = open(Contacts.fileName, 'wb')
pickle.dump(contacts, f)
f.close()
print ("{0} was deleted successfully".format(name))
except KeyError:
print("The name you entered is not in the contact list")
except EOFError:
print("There are no contacts in your contact list")
######################################################
######## Update Contacts ##################
######################################################
def update(self, name, number):
try:
f = open(Contacts.fileName, 'rb')
contacts = pickle.load(f)
f.close()
if contacts[name]:
contacts[name] = number
f = open(Contacts.fileName, 'wb')
pickle.dump(contacts, f)
f.close()
print("{0} was modified successfully ".format(name))
else:
print ("{0} does not exist in the contacts".format(name))
except KeyError:
print("The name you entered is not in the contact list")
except EOFError:
print("There are no contacts in your contact list")