-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphonebook.py
141 lines (115 loc) · 3.74 KB
/
phonebook.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
import pickle
import os
import pathlib
class Phonebook:
# class variables
first = ''
last = ''
num = 0
def addNum(self):
self.first = input('enter first name: ')
self.last = input('enter last name: ')
self.num = int(input('enter phone number: '))
def create():
book = Phonebook()
book.addNum()
writedatatofile(book)
def writedatatofile(book):
file = pathlib.Path('Phonebook.data')
if file.exists():
infile = open('Phonebook.data', 'rb')
oldlist = pickle.load(infile)
oldlist.append(book)
infile.close()
os.remove('Phonebook.data')
else:
oldlist = [book]
outfile = open('newphonebook.data', 'wb')
pickle.dump(oldlist, outfile)
outfile.close()
os.rename('newphonebook.data', 'Phonebook.data')
def showcontacts():
file = pathlib.Path('Phonebook.data')
if file.exists():
infile = open('Phonebook.data', 'rb')
mylist = pickle.load(infile)
for item in mylist:
print('first name lastname number')
print(item.first, ' ', item.last, ' ', item.num)
infile.close()
else:
print('no data found')
def modifycontact(name):
file = pathlib.Path('Phonebook.data')
if file.exists():
infile = open('Phonebook.data', 'rb')
oldlist = pickle.load(infile)
infile.close()
os.remove('Phonebook.data')
for item in oldlist:
if item.first == name:
item.first = input('enter first name: ')
item.last = input('enter last name: ')
item.num = int(input('enter phone number'))
else:
print('no data found')
outfile = open('newphonebook.data', 'wb')
pickle.dump(oldlist, outfile)
outfile.close()
os.rename('newphonebook.data', 'Phonebook.data')
def searchcontact(name):
file = pathlib.Path('Phonebook.data')
infile = open('Phonebook.data', 'rb')
mylist = pickle.load(infile)
infile.close()
found = False
for item in mylist:
if item.first == name:
print('number: ', item.num)
found = True
else:
print('no data found')
def deletecontact(name, lastname):
file = pathlib.Path('Phonebook.data')
infile = open('Phonebook.data', 'rb')
oldlist = pickle.load(infile)
infile.close()
newlist = []
for item in oldlist:
if item.first != name and item.last != lastname:
newlist.append(item)
os.remove('Phonebook.data')
outfile = open('newphonebook.data', 'wb')
pickle.dump(newlist, outfile)
outfile.close()
os.rename('newphonebook.data', 'Phonebook.data')
select = ''
while select != 6:
print('welcome to phoenbook')
print('\t1. add contact')
print('\t2. show contacts')
print('\t3. edit contact')
print('\t4. search contact')
print('\t5. delete contact')
print('\t6. exit')
select = input()
if select == '1':
create()
print('contact create successfully')
elif select == '2':
showcontacts()
elif select == '3':
name = input('enter name of the contact: ')
modifycontact(name)
print('contact modified successfully')
elif select == '4':
name = input('enter name of the contact: ')
searchcontact(name)
elif select == '5':
name = input('enter first name: ')
lastname = input('enter last name: ')
deletecontact(name, lastname)
print('contact delete successfully')
elif select == '6':
print('thank you')
break