A binary file "Student.dat" has structure [Admno, Stud_Name, Stream, Fees].
5
i.
Write a user defined function Create File() to input data for a record and add
to Student.dat.
ii.
Write a function CountRec(Stream) in Python which accepts the Stream
name as parameter and count and return number of students in the given
stream are stored in the binary file “Student.dat by".
Answers
Answer:
1)
def File():
import pickle as p
with open("Student.dat","wb") as r:
x=int(input("How many values you want to input="))
y=[]
for i in range(x):
yy=[]
w=int(input("Enter Admno="))
ww=input("Enter Stud_Name=")
www=input("Enter Stream=")
wwww=int(input("Enter Fees="))
yy.append(w)
yy.append(ww)
yy.append(www)
yy.append(wwww)
y.append(yy)
print()
p.dump(y,r)
File()
2)
def CountRec(Stream):
import pickle as p
with open("Student.dat","rb") as r:
y=p.load(r)
print(y)
c=0
for i in y:
if i[2]==Stream:
c=c+1
print("Nimber of students with stream ",Stream,"are",c)
x=input("Enter stream to search=")
CountRec(x)
Explanation: