-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathPRACTICAL 5.py
116 lines (92 loc) · 2.59 KB
/
PRACTICAL 5.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
import pickle
#file_name to be used wherever required
file_name = 'data.dat'
#function to create and initialise the binary file
def create_blank_file( ):
f = open(file_name, 'wb')
f.close()
# function to input the values from user and return the record
def input_rec( ):
rec={}
rec['rollno'] = input('Enter Roll No')
rec['name'] = input('Enter name')
rec['marks'] = int(input('Enter marks'))
return rec
# function to add record into file input the values from user and return the record
def add_record( ):
lst=[]
#Load all records into list
f = open(file_name,'rb')
while True:
try:
lst.append(pickle.load(f))
except:
break
f.close()
#Add new record into list
rec=input_rec()
lst.append(rec)
#Save all records to file
fout = open(file_name,'wb')
for rec in lst:
pickle.dump(rec,fout)
fout.close()
# function to show the values to display
def show_rec(rec):
output='{0},{1},{2}'.format(rec['rollno'], rec['name'], rec['marks'])
print(output)
#function to show the All records of binary file
def show_all( ):
fin = open(file_name , 'rb')
while True:
try:
rec=pickle.load(fin)
show_rec(rec)
except:
break
fin.close( )
# function to input rollno , marks and update the marks
def update_marks( ):
f = open(file_name, 'rb')
rno = input('Enter Rollno')
l = [] #List of records to hold data in memory
found=False
while True:
try:
rec=pickle.load(f)
if rec['rollno'] == rno:
found=True
print('The old record:')
show_rec(rec)
m = int(input('Enter new marks'))
rec['marks'] = m #update record in memory
l.append(rec) # Add all records in list including updated
except:
break
f.close() #Close for reading
if found: #if record updated
fout = open(file_name,'wb')
for rec in l:
pickle.dump(rec,fout) # Store the record
fout.close( )
def menu():
print('1. Create File for data store')
print('2. Add Record')
print('3. Show Records')
print('4. Update Marks')
print('5. Exit')
ch = input('Enter your response:')
if ch in '12345':
return int(ch)
else:
return 0
def main( ):
functions=[create_blank_file,add_record,show_all,update_marks]
exit_option = 5
while(True):
ch=menu()
if ch == exit_option:
break
if ch > 0 and ch < 6:
func = functions[ch-1]
func( )