Binary File Exam Based Questionyyuiis
Binary File Exam Based Questionyyuiis
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 student.dat has structure A binary file “STUDENT.DAT” has structure
(rollno,name,class,percentage). Write a program (admission_number, Name, Percentage). Write a
to updating a record in the file requires roll function countrec() in Python that would read
number to be fetched from the user whose name contents of the file “STUDENT.DAT” and display
is to be updated the details of those students whose percentage is
import pickle above 75. Also display number of students
import os scoring above 75%
f1 = open(‘student.dat','rb')
f2=open(“temp.dat”,”wb”) import pickle
r=int(input(“enter rollno which you want to def CountRec():
search”)) f=open("STUDENT.DAT","rb")
try: num = 0
while True: try:
e = pickle.load(f1) while True:
if e[0]==r: rec=pickle.load(f)
e[1]=input(“enter name”) if rec[2] > 75:
pickle.dump(e,f2) print(rec[0],rec[1],rec[2])
else: num = num + 1
pickle.dump(e,f2) except:
except: f.close()
f1.close() return num
f2.close()
os.remove(“student.dat”)
os.rename(“temp.dat”,”student,dat”)
A binary file named “EMP.dat” has some records A binary file “Items.dat” has structure as [ Code,
of the structure [EmpNo, EName, Post, Salary] Description, Price ].
(a) Write a user-defined function named i. Write a user defined function MakeFile( ) to
NewEmp() to input the details of a new employee input multiple items from the user and add to
from the user and store it in EMP.dat. Items.dat
(b) Write a user-defined function named ii. Write a function SearchRec(Code) in Python
SumSalary(Post) that will accept an argument the which will accept the code as parameter and
post of employees & read the contents of search and display the details of the
EMP.dat and calculate the SUM of salary of all corresponding code on screen from Items.dat.
employees of that Post. (i)
(a) import pickle
import pickle def MakeFile( ):
def NewEmp ( ): while True:
f = open(“EMP.dat”,”wb”) code = input(“Enter Item Code :”)
EmpNo = int(input(“Enter employee desc = input(“Enter description :”)
number: “)) price = float(input(“Enter price:”))
EName = input(“Enter name:”) d= [code,desc,price]
Post = input(“Enter post:”) f = open (“Items.dat”, “ab”)
Salary = int(input(“Enter salary”)) pickle.dump( d,f )
rec = [EmpNo, Ename, Post,Salary] ch = input(“Add more record? (y/n) :”)
pickle.dump(rec, f) if ch==’n’:
f.close() break
(b) f.close( )
def SumSalary(Post): (ii)
f = open("EMP.dat", "rb") def SearchRec(code):
c=0 f = open("Items.dat", "rb")
while True: found = False
try: while True:
g = p.load(f) try:
if g[2]==Post: g = p.load(f)
c=c+g[3] if g[0]==code:
except: print(g[0],g[1],g[2])
f.close() found=True
print("sum of salary", c) break
except:
if found == False:
print("No such record")
f.close()
A binary file named “TEST.dat” has some records Consider a binary file emp.dat having records in
of the structure [TestId, Subject, MaxMarks, the form of dictionary. E.g {eno:1, name:”Rahul”,
ScoredMarks] Write a function in Python named sal: 5000} write a python function to display the
DisplayAvgMarks(Sub) that will accept a subject records of above file for those employees who
as an argument and read the contents of get salary between 25000 and 30000
TEST.dat. The function will calculate & display the
Average of the ScoredMarks of the passed
Subject on screen.
def SumSalary(Sub): import pickle
f = open("ABC.dat", "rb") def search():
c=0 f=open(“emp.dat”,”rb”)
s=0 while True:
while True: try:
try: d=pickle.load(f)
g = p.load(f) if(d[‘sal’]>=25000 and
print(g) d[‘sal’]<=30000):
if g[1]==Sub: print(d)
s=s+g[3] except EOFError:
c=c+1 break
except: f.close()
f.close()
print("sum of salary", s/c)
f.close()
A binary file “Bank.dat” has structure as Consider an employee data, Empcode, empname
[account_no, cust_name, balance]. and salary.
i. Write a user-defined function addfile( ) and add (i) Write python function to create
a record to Bank.dat. binary file emp.dat and store their
ii. Create a user-defined function CountRec( ) to records.
count and return the number of customers (ii) write function to read and display all
whose balance amount is more than 100000. the records
(i) Ans
import pickle import pickle
def addfile( ): def add_record():
f = open(“bank.dat”,”wb”) f = open(“emp.dat”,”ab”)
acc_no = int(input(“Enter account empcode =int(input(“employee code:”))
number: “)) empname = int(input(“empName:”))
cust_name = input(“Enter name:”) salary = int(input(“salary:”))
bal = int(input(“Enter balance”)) d = [empcode, empname, salary]
rec = [acc_no, cust_name, bal] pickle.dump(d,f)
p.dump(rec, f) f.close()
f.close() import pickle
(ii)
def CountRec( ): def search():
f = open(“bank.dat”,”rb”) f=open(“emp.dat”,”rb”)
c=0 while True:
try: try:
while True: d=pickle.load(f)
rec = p.load(f) print(d)
if rec[2] > 100000: except EOFError:
c += 1 break
except: f.close()
f.close()
return c
Write a function SCOUNT( ) to read the content Given a binary file “emp.dat” has structure
of binary file “NAMES.DAT‟ and display number (Emp_id, Emp_name, Emp_Salary). Write a
of records (each name occupies 20 bytes in file ) function in Python countsal() in Python that
where name begins from “S‟ in it would read contents of the file “emp.dat” and
def SCOUNT( ): display the details of those employee whose
s=' ' salary is greater than 20000
count=0 import pickle
f=open('Names.dat', 'rb'): def countsal():
while True: f = open (“emp.dat”, “rb”)
s = f.read(20) n=0
if len(s)!=0: try:
if s[0].lower()=='s': while True:
count+=1 rec = pickle.load(f)
print('names beginning from "S" are ',count) if rec[2] > 20000:
print(rec[0], rec[1], rec[2])
n=n+1
except:
print(n)
f.close()
Write Python function DISPEMP( ) to read the Consider the following CSV file (emp.csv):
content of file emp.csv and display only those Sl,name,salary
records where salary is 4000 or above 1,Peter,3500
import csv 2,Scott,4000
def DISPEMP(): 3,Harry,5000
csvfile=open('emp.csv'): 4,Michael,2500
myreader = csv.reader(csvfile,delimiter=',') 5,Sam,4200
print(EMPNO,EMP NAME,SALARY) Write Python function DISPEMP( ) to read the
for row in myreader: content of file emp.csv and display only those
if int(row[2])>4000: records where salary is 4000 or above
print(row[0], row[1],row[2]) import csv
def DISPEMP():
csvfile=open('emp.csv'):
myreader = csv.reader(csvfile,delimiter=',')
print(EMPNO,EMP NAME,SALARY)
for row in myreader:
if int(row[2])>4000:
print(row[0], row[1],row[2])
A binary file “Stu.dat” has structure (rollno, A binary file “Stu.dat” has structure (rollno,
name, marks). name, marks).
Write a function in Python add_record() to input
data for a record and add to Stu.dat.
import pickle Write a function in python Search_record() to
def add_record(): search a record from binary file “Stu.dat” on the
fobj = open(“Stu.dat”,”ab”) basis of roll number.
rollno =int(input(“Roll no:”)) def Search_record():
name = int(input(“Name:”)) f = open(“Stu.dat”, “rb”)
marks = int(input(“Marks:”)) stu_rec = pickle.load(f)
data = [rollno, name, marks] found = 0
pickle.dump(data,fobj) rno = int(input(“roll number to search:”))
fobj.close() try:
for R in stu_rec:
if R[0] == rno:
print (R[1], “Found!”)
found = 1
break
except:
if found == 0:
print (“Sorry, record not found:”)
f.close()