-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbinary_operations_file.py
190 lines (168 loc) · 5.61 KB
/
binary_operations_file.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
'''
Create a binary file with name and roll number. Search for a given roll number and display
the name, if not found display appropriate message.
Create a binary file with roll number, name and marks. Input a roll number and update the marks.
'''
import pickle,os
#Function to append records to student.dat file
def Append(l):
fwb=open("student.dat","ab")
pickle.dump(l,fwb)
fwb.close()
print("Data succsessfully written to binary file")
#Function to display all records stored in student.dat file
def readall():
if os.path.isfile("student.dat"): #file exists
frb=open("student.dat","rb")
print("Contents of student.dat file...")
while True:
try:
l=pickle.load(frb)
print(l)
except EOFError:
break
frb.close()
else:
print("File does not exist")
#Function to search and display the student record roll number wise
def search_rno(r):
if os.path.isfile("student.dat"): #file exists
frb=open("student.dat","rb")
found=0
while True:
try:
l=pickle.load(frb)
if r==l[0]:
print(l)
found=1
break #because rno is unique
except EOFError:
break
if found==0:
print("Roll no",r,"does not exist.")
frb.close()
else:
print("File does not exist")
#Function to search and display the student record name wise
def search_name(n):
if os.path.isfile("student.dat"): #file exists
frb=open("student.dat","rb")
found=0
while True:
try:
l=pickle.load(frb)
if n==l[1]: #made changes
print(l)
found=1
except EOFError:
break
if found==0:
print("Name",n,"does not exist.")
frb.close()
else:
print("File does not exist")
#Function to search the student record on roll number and update the name and marks as
#required by the user.
def update(r):
if os.path.isfile("student.dat"): #file exists
frb=open("student.dat","rb")
fwb=open("temp.dat","wb")
found=0
while True:
try:
l=pickle.load(frb)
if r==l[0]:
ans1=input("Update name?")
if ans1=='y' or ans1=='Y':
l[1]=input("Enter the new name")
ans2=input("Update marks?")
if ans2=='y' or ans2=='Y':
l[2]=int(input("Enter the new marks"))
#dump the updated record in temp.dat file
pickle.dump(l,fwb)
found=1
else:
pickle.dump(l,fwb)
except EOFError:
break
frb.close()
fwb.close()
os.remove("student.dat")
os.rename("temp.dat","student.dat")
if found==0:
print("Roll no",r,"does not exist.")
else:
print("Record updated....")
readall()
else:
print("File does not exist")
def delete(r):
if os.path.isfile("student.dat"): #file exists
frb=open("student.dat","rb")
fwb=open("temp.dat","wb")
found=0
while True:
try:
l=pickle.load(frb)
if r!=l[0]: #record not be deleted
pickle.dump(l,fwb)
else:
found=1
except EOFError:
break
frb.close()
fwb.close()
os.remove("student.dat")
os.rename("temp.dat","student.dat")
if found==0:
print("Roll no",r,"does not exist.")
else:
print("Record deleted....")
readall()
else:
print("File does not exist")
def searchmenu():
while True:
print("--------------SEARCH MENU----------------")
print("1. Search by roll no")
print("2. Search by name")
print("3. Return to Main Menu")
ch=int(input("Enter your choice:"))
if ch==1:
rno=int(input("Enter the roll no whose record is to be searched:"))
search_rno(rno)
elif ch==2:
nm=input("Enter the name whose record is to be searched:")
search_name(nm)
elif ch==3:
break
def mainmenu():
while True:
print("--------------BINARY FILE OPERATIONS----------------")
print("1. Append records")
print("2. Display all records")
print("3. Search Records")
print("4. Update Records")
print("5. Delete Records")
print("6. EXIT")
ch=int(input("Enter your choice:"))
if ch==1:
print("Enter the student record")
rno=int(input("Enter the roll no:"))
name=input("Enter the name:")
marks=int(input("Enter the marks:"))
rec=[rno,name,marks]
Append(rec)
elif ch==2:
readall()
elif ch==3:
searchmenu()
elif ch==4:
rn=int(input("Enter the roll no whose record is to be updated:"))
update(rn)
elif ch==5:
rn=int(input("Enter the roll no whose record is to be deleted:"))
delete(rn)
elif ch==6:
break
mainmenu()