Binary File Questions
Binary File Questions
def countrec(Author):
fobj=open("Book.dat", "rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
if Author==rec[2]:
num = num + 1
print(rec[0],rec[1],rec[2],rec[3])
except:
fobj.close()
return num
n=countrec("amit") # This function is called just to verify
result and not required in exam
print("Total records", n) # This statement is just to verify
result and not required in exam
Q2. A binary file “STUDENT.DAT” has structure
[admission_number, Name, Percentage]. Write a function
countrec() in Python that would read contents of the file
“STUDENT.DAT” and display the details of those students
whose percentage is above 75. Also display number of students
scoring above 75%.
import pickle
def countrec():
fobj=open("student.dat","rb")
num = 0
try:
while True:
rec=pickle.load(fobj)
if rec[2]>75:
num = num + 1
print(rec[0],rec[1],rec[2])
except:
fobj.close()
return num
https://csiplearninghub.com/questions-of-binary-file-handling-in-python