-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary operations.py
More file actions
110 lines (105 loc) · 3.09 KB
/
Binary operations.py
File metadata and controls
110 lines (105 loc) · 3.09 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
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
# Write a menu driven program as following:
#
# Insert record
# Search Record
# Update Record
# Display record
# Exit
# Use cust.dat file for this program and make sure the formal address of file is specified in the program properly
import pickle
def insert():
print()
with open(r"C:\Users\Administrator\Desktop\txt files\Cust.dat", "ab") as file:
di = input("Enter ID: ")
nm = input("Enter Name: ")
city = input("Enter City: ")
pickle.dump([di, nm, city], file)
print()
def search():
print()
with open(r"C:\Users\Administrator\Desktop\txt files\Cust.dat", "rb") as file:
flag = False
se = input("Enter ID to search: ")
while True:
try:
data = pickle.load(file)
if data[0] == se:
flag = True
save = data
except:
break
if flag == True:
print("Value Found.")
print(save)
else:
print("Value Not found.")
print()
def update():
print()
l=[]
with open(r"C:\Users\Administrator\Desktop\txt files\Cust.dat", "rb") as file:
flag = False
se = input("Enter ID to search: ")
while True:
try: #try block for error handling
data = pickle.load(file)
l.append(data)
except:
break
with open(r"C:\Users\Administrator\Desktop\txt files\Cust.dat", "wb") as file:
di = input("Enter New ID: ")
nm = input("Enter New Name: ")
c = input("Enter New City: ")
pickle.dump([di,nm,c],file)
for i in l:
if se != i[0]:
pickle.dump(i,file)
print("Updated.")
print()
def display():
print()
with open(r"C:\Users\Administrator\Desktop\txt files\Cust.dat", "rb") as file:
while True:
try:
data = pickle.load(file)
print(data)
except:
break
print()
def delete():
print()
l=[]
with open(r"C:\Users\Administrator\Desktop\txt files\Cust.dat", "rb") as file:
se = input("Enter ID to search: ")
while True:
try:
l.append(pickle.load(file))
except:
break
with open(r"C:\Users\Administrator\Desktop\txt files\Cust.dat", "wb") as file:
for i in l:
if se != i[0]:
pickle.dump(i,file)
# menu for available options and services.
while True:
print("1 <---- Insert a new data.")
print("2 <---- Update an existing data.")
print("3 <---- Display data.")
print("4 <---- Search Data.")
print("5 <---- Delete Data.")
print('6 <---- Exit.')
ch = int(input("Enter YOur choice: "))
if (ch == 1):
insert()
elif (ch == 2):
update()
elif (ch == 3):
display()
elif (ch == 4):
search()
elif (ch == 5):
delete()
elif (ch == 6):
break
else:
print("Invalid Choice!!")