-
Notifications
You must be signed in to change notification settings - Fork 0
/
superphonebook2.py
200 lines (156 loc) · 6.1 KB
/
superphonebook2.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# superphonebook.py
# Super phone book to search, insert, delete, update, show all
'''Populate phone book with existing contacts when program starts'''
def initialize():
try:
# open input file
infile = open("CONTACTS.TXT", "r")
# read contents
lines = infile.readlines()
for line in lines: # for each contact
line = line.rstrip("\n") # remove trailing new line character
name = line[:20].strip() # get name with trailing white space removed
contact_no = line [20:] # get contact number
phonebook[name] = contact_no # insert to phonebook dictionary
except IOError: # file read error
print("CONTACTS.TXT does not exist? Starting with empty phone book.")
'''Menu to display options'''
def menu():
print("Welcome to Super Phone Book")
print("(1) Search")
print("(2) Insert")
print("(3) Update")
print("(4) Delete")
print("(5) Show all")
print("(6) Import")
print("(7) Export")
print("(0) Quit")
'''Search name and return contact number if found, else return not found'''
def search(search_name):
if search_name in phonebook.keys(): # found, show name and phone
print("Contact number for", search_name, "is", phonebook[search_name])
else: # not found
print("No such contact")
'''Insert entry with name and contact number if not found, else prompt for update'''
def insert(insert_name):
if insert_name not in phonebook.keys(): # not found, insert
contact_no = input("Enter contact number: ")
phonebook[insert_name] = contact_no
print("Entry added")
else: # found, prompt for update
update_ans = input("Already exists! Update(y/n)? ")
if update_ans == 'y':
update(insert_name)
'''Update entry with new contact number if found, else prompt for insert'''
def update(update_name):
if update_name in phonebook.keys(): # found, update
print("Current contact number for", update_name, "is", phonebook[update_name])
contact_no = input("Enter new contact number: ")
phonebook[update_name] = contact_no
print("Entry updated.")
else: # not found, prompt for insert
insert_ans = input("Contact does not exist! Insert(y/n) ")
if insert_ans == 'y':
insert(update_name)
'''Delete entry if found, else error'''
def delete(delete_name):
if delete_name in phonebook.keys(): # found, delete
del phonebook[delete_name]
print("Entry deleted.")
else: # not found, error
print("Contact not found! Nothing to delete.")
'''Display all names and contact numbers'''
def show_all():
print("Name | Contact No") # heading
for key, value in phonebook.items():
print(key, value) # contact name and number
print(len(phonebook), "contacts in phone book.") # number of records
'''Import contacts from text file'''
def import_contacts():
successful_import = 0
try:
# open input contacts file
infile = open("NEWCONTACTS.TXT", "r")
# read all contents
lines = infile.readlines()
for line in lines: # for each contact
line = line.rstrip("\n") # remove trailing new line character
name = line[:20].strip() # get name with trailing white space removed
contact_no = line [20:] # get contact number
if name not in phonebook.keys(): # if not exist, insert and increment successful import
phonebook[name] = contact_no
successful_import = successful_import + 1
# show import status
print(successful_import, "contacts imported.")
if successful_import != len(lines):
print(len(lines) - successful_import, "contacts already exist.")
# close input file
infile.close()
except IOError: # file read error
print("NEWCONTACTS.TXT does not exist! Cannot import.")
'''Export contacts to text file'''
def export_contacts():
try:
# open output contacts file
outfile = open("CONTACTS.TXT", "w")
# write formatted names and contact numbers to file
for key, value in phonebook.items():
outfile.write("{0:20s}{1}\n".format(key, value))
# show export status
print(len(phonebook), "contacts exported.")
# close output file
outfile.close()
except IOError: # file write error
print("Unable to export. Maybe disk full?")
'''Populate CONTACTS.TXT with existing contacts when program ends'''
def finalize():
try:
# open output file
outfile = open("CONTACTS.TXT", "w")
# write formatted names and contact numbers to file
for key, value in phonebook.items():
outfile.write("{0:20s}{1}\n".format(key, value))
# close output file
outfile.close()
except IOError: # file write error
print("Unable to export. Maybe disk full?")
# main
# initialize an empty dictionary
phonebook = {}
# populate phone book with existing contacts when program starts
initialize()
# assume not done
done = False
# loop until user chooses to quit
while not done:
# display menu
menu()
# get choice from user
option = input("What do you want to do today? ")
if option == '1': # search
name = input("Enter name to search: ")
search(name)
elif option == '2': # insert
name = input("Enter name to insert: ")
insert(name)
elif option == '3': # update
name = input("Enter name to update: ")
update(name)
elif option == '4': # delete
name = input("Enter name to delete: ")
delete(name)
elif option == '5': # show all
show_all()
elif option == '6': # import contacts
import_contacts()
elif option == '7': # export contacts
export_contacts()
elif option == '0': # quit
done = True
else: # invalid choices
print("Invalid option! Try again.")
print()
# populate CONTACTS.TXT with existing contacts when program ends
finalize()
# exit
print("Bye.")