A binary file “student.dat” has structure [rollno, A binary file “emp.dat” has structure [EID,
name, marks]. Ename, designation, salary].
i. Write a user defined function insertRec() to i. Write a user defined function CreateEmp() to
input data for a student and add to student.dat. input data for a record and create a file emp.dat.
ii. Write a function searchRollNo( r ) in Python ii. Write a function display() in Python to display
which accepts the student’s rollno as parameter the detail of all employees whose salary is more
and searches the record in the file “student.dat” than 50000.
and shows the details of student i.e. rollno, name (i)
and marks (if found) otherwise shows the import pickle
message as ‘No record found’. def CreateEmp():
f1=open("emp.dat",'wb')
(i) eid=input("Enter E. Id")
import pickle ename=input("Enter Name")
def insertRec(): designation=input("Enter Designation")
f=open("student.dat","ab") salary=int(input("Enter Salary"))
rollno = int (input("Enter Roll Number : ")) l=[eid,ename,designation,salary]
name=input("Enter Name :") pickle.dump(l,f1)
marks = int(input("Enter Marks : ")) f1.close()
rec = [rollno, name, marks ] (ii)
pickle.dump( rec, f ) import pickle
f.close() def display():
(ii) f2=open("emp.dat","rb")
def searchRollNo( r ): try:
f=open("student.dat","rb") while True:
flag = False rec=pickle.load(f2)
while True: if rec[3]>5000:
try: print(rec[0],rec[1],rec[2],rec[3])
rec=pickle.load(f) except:
if rec[0] == r : f2.close()
print(rec[‘Rollno’])
print(rec[‘Name’])
print(rec[‘Marks])
flag == True
except EOFError:
break
if flag == False:
print(“No record Found”)
f.close()
Write a python program to append a new records Write a python program to search and display the
in a binary file –“student.dat”. The record can record of the student from a binary file
have Rollno, Name and Marks. “Student.dat” containing students records
import pickle (Rollno, Name and Marks). Roll number of the
while True: student to be searched will be entered by the
rollno = int(input("Enter your rollno: ")) user.
name = input("Enter your name: ")
, marks = int(input("enter marks obtained: ")) import pickle
d = [rollno, name, marks] f1 = open("Student.dat", "rb")
f1 = open("Student.dat", "wb") rno = int(input(“Enter the roll no to search: ”))
pickle.dump(d, f1) flag = 0
choice = input("enter more records: y/n") try:
if choice== "N": while True:
break r = pickle.load(f1)
f1.close() if rno == r[0]:
print (rollno, name, marks)
flag = 1
except:
if flag == 0:
print(“Record not found…”)
f1.close()
i. A binary file “emp.DAT” has structure (EID, A binary file named “EMP.dat” has some records
Ename, designation,salary). Write a function to of the structure [EmpNo, EName, Post, Salary]
add more records of employes in existing file (a) Create a binary file “EMP.dat” that stores the
emp.dat. records of employees and display them one by
ii. Write a function Show() in Python that would one.
read detail of employee from file “emp.dat” and (b) Display the records of all those employees
display the details of those employee whose who are getting salaries between 25000 to
designation is “Salesman”. 30000.
(i) (a)
import pickle import pickle
def createemp: f1 = open('emp.dat','rb')
f1=open("emp.dat",'ab') try:
eid=input("Enter E. Id") while True:
ename=input("Enter Name") e = pickle.load(f1)
designation=input("Enter Designation") print(e)
salary=int(input("Enter Salary")) except:
l=[eid,ename,designation,salary] f1.close()
pickle.dump(l,f1)
f1.close() (b)
(ii) import pickle
def display(): f1 = open('emp.dat','rb')
f2=open("emp.dat","rb") try:
try: while True:
while True: e = pickle.load(f1)
rec=pickle.load(f2) if(e[3]>=25000 and e[3]<=30000):
if (rec[2]=='Manager'): print(e)
print(rec[0],rec[1], rec[2],rec[3]) except:
except: f1.close()
break
f2.close()
A binary file “Book.dat” has structure [BookNo,
Book_Name, Author, Price].