0% found this document useful (0 votes)
6 views8 pages

Binary File Handling in Python

The document outlines various user-defined functions for handling binary files in Python, specifically for managing employee records stored in 'employee.dat' and 'emp.dat'. Functions include displaying records, calculating total and average salaries, filtering by department and gender, and searching for specific employees. It also covers copying records to a new file and finding employees with maximum and minimum salaries based on different criteria.

Uploaded by

pravinyt800
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Binary File Handling in Python

The document outlines various user-defined functions for handling binary files in Python, specifically for managing employee records stored in 'employee.dat' and 'emp.dat'. Functions include displaying records, calculating total and average salaries, filtering by department and gender, and searching for specific employees. It also covers copying records to a new file and finding employees with maximum and minimum salaries based on different criteria.

Uploaded by

pravinyt800
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Class – XII Chapter – 5 (File Handling – Binary File Programs)Computer Science - 083

File Name : [Link]. Structure - [empid, name, gender, dept, salary]


1. Write a user defined function named display() to display the records and also
the number of records from the file.
import pickle
def writebin():
f=open('[Link]','wb')
data=[[101,"John Smith","Male","Sales",60000],
[102,"Alice Johnson","Female","Marketing",70000],
[103,"Bob Williams","Male","Engineering",80000],
[104,'Mariya jose','Female','Purchase',34000],
[105,"Emily Davis", "Female","Sales",65000],
[106,"George", "Male","Purchase",35000],
[107,"David Brown", "Male","Marketing",72000]]
for i in data:
[Link](i,f)
[Link]()
writebin()
def display():
f = open("[Link]", 'rb')
c=0
while True:
try:
data = [Link](f)
print(data)
c=c+1
except EOFError:
if c== 0:
print("No records found")
else:
print("Number of records:", c)
break
[Link]()
display()
2. Write a user defined function named totalsal() to display the sum of the
salaries of all the employees
import pickle
def totalsal():
f = open("[Link]", 'rb')
c=0
s=0
while True:
try:
data = [Link](f)
s = s + data[-1] #data[-1] or data[4]
c=c+1
except EOFError:
if c== 0:
print("No records found")
else:
print("Total Salary of all the employees:", s)
break
[Link]()
totalsal()
3. Write a user defined function named avgsal() to display the Average salary of
all the employees
Page 1 of 8
Class – XII Chapter – 5 (File Handling – Binary File Programs)Computer Science - 083
import pickle
def avgsal():
f = open("[Link]", 'rb')
c=0
s=0
while True:
try:
data = [Link](f)
s = s + data[-1] #data[-1] or data[4]
c=c+1
except EOFError:
if c== 0:
print("No records found")
else:
print("Average Salary of all the employees:", s/c)
break
[Link]()
avgsal()
4. Write a user defined function named avgsalPurchase() to display the Average salary
of employees from “Purchase” department.
import pickle
def avgsalPurchase():
f = open("[Link]", 'rb')
c=0
s=0
while True:
try:
data = [Link](f)
if data[3] == 'Purchase':
s = s + data[-1] #data[-1] or data[4]
c=c+1
except EOFError:
if c== 0:
print("No records found")
else:
print("Average Salary of Purchase Dept employees:", s/c)
break
[Link]()
avgsalPurchase()
5. Write a user defined function named maleemployee() to display the names of all the
male employees.
import pickle
def maleemployee():
f = open("[Link]", 'rb')
c=0
while True:
try:
data = [Link](f)
if data[2] == 'Male':
print(data[1])
c+=1
except EOFError:
if c== 0:
print("No records found")
else:
Page 2 of 8
Class – XII Chapter – 5 (File Handling – Binary File Programs)Computer Science - 083
print("Total Records:", c)
break
[Link]()
maleemployee()
6. Write a user defined function named salary() to display the name and salary of all
the female employees whose salary is more than 60000.
import pickle
def salary():
f = open("[Link]", 'rb')
c=0
while True:
try:
data = [Link](f)
if data[2] == 'Female' and data[-1] > 60000:
print(data[1], data[-1])
c+=1
except EOFError:
if c== 0:
print("No records found")
else:
print("Total Records:", c)
break
[Link]()
salary()
7. Write a user defined function named count_emp() to display the number of male
and female employees from the file.
import pickle
def count_emp():
f = open("[Link]", 'rb')
male = 0
female = 0
while True:
try:
data = [Link](f)
if data[2] == 'Male' :
male+=1
elif data[2] == 'Female':
female+=1
except EOFError:
if male == 0 and female == 0:
print("No records found")
else:
print("No. of Male Employees:", male)
print("No. of Female Employees:", female)
break
[Link]()
count_emp()
8. Write a user defined function named search(empid) which accepts empid as
parameter and searches and prints the record of the employee whose empid is given
in the parameter.
import pickle
def search(empid) :
f = open("[Link]", 'rb')
c=0
while True:
Page 3 of 8
Class – XII Chapter – 5 (File Handling – Binary File Programs)Computer Science - 083
try:
data = [Link](f)
if data[0] == empid :
print(data)
c += 1
except EOFError:
if c == 0:
print("No records found")
break
[Link]()
empid=int(input('Enter Empid to search'))
search(empid)
9. Write a user defined function named search(salary) which accepts salary as
parameter and searches and prints the record of those employees whose salary is
more than the value passed as parameter “Salary” in the function.
import pickle
def search(salary) :
f = open("[Link]", 'rb')
c=0
while True:
try:
data = [Link](f)
if data[-1] > salary :
print(data)
c += 1
except EOFError:
if c == 0:
print("No records found")
else:
print("No. of records:",c)
break
[Link]()
salary=int(input('Enter Salary'))
search(salary)
10. Write a user defined function named copyPurchase() which copies the records of
employees who belong to “Purchase” department to a new file named
“[Link]”
import pickle
def copyPurchase() :
f1 = open("[Link]", 'rb')
f2 = open("[Link]", 'wb')
c=0
while True:
try:
data = [Link](f1)
if data[3] == 'Purchase' :
[Link](data, f2)
c += 1
except EOFError:
if c == 0:
print("No records found")
else:
print("No. of records:",c)
break
[Link]()
Page 4 of 8
Class – XII Chapter – 5 (File Handling – Binary File Programs)Computer Science - 083
[Link]()
f=open("[Link]", 'rb')
while True:
try:
data = [Link](f)
print(data)
except EOFError:
if c == 0:
print("No records found")
break
[Link]()
copyPurchase()
Dicitonary Structure. File Name : [Link].
Structure – {empid:[ name, gender, dept, salary] }
11. Write a user defined function named search() which would get employee id as
input and print the record i.e [ name, gender, dept, salary] that matches the
entered employee id from the file.
import pickle
def writebin():
f=open('[Link]','wb')
data={101:["John Smith","Male","Sales",60000],
102:["Alice Johnson","Female","Marketing",70000],
103:["Bob Williams","Male","Engineering",80000],
104:['Mariya jose','Female','Purchase',34000],
105:["Emily Davis", "Female","Sales",65000],
106:["George", "Male","Purchase",35000],
107:["David Brown", "Male","Marketing",72000]}
[Link](data,f)
[Link]()
writebin()
import pickle
def search() :
f = open("[Link]", 'rb')
c=0
empid = int(input("Enter the employee ID to be searched:"))
while True:
try:
data = [Link](f)
for i in data: #traverse on dictionary-i gets empid
if i == empid:
print(data[i])
c += 1
except EOFError:
if c == 0:
print("No records found")
else:
print("No. of records:",c)
break
[Link]()
search()
12.
Write a user defined function named salesEmployee() which would get print the
empid and names of employees from Sales department.
import pickle
def salesEmployee() :
Page 5 of 8
Class – XII Chapter – 5 (File Handling – Binary File Programs)Computer Science - 083
f = open("[Link]", 'rb')
c=0
while True:
try:
data = [Link](f)
for i in data: #traverse on dictionary-i gets empid
if data[i][2] == 'Sales':
print(i, data[i][0])
c += 1
except EOFError:
if c == 0:
print("No records found")
else:
print("No. of records:",c)
break
[Link]()
salesEmployee()
13. Write a user defined function named maxSalary() which display the record of
the employee with maximum salary.
import pickle
def maxSalary() :
f = open("[Link]", 'rb')
c=0
max_value = 0
while True:
try:
data = [Link](f)
for i in data:
if c==0:
max_value = data[i][-1]
rec={i:data[i]}
elif data[i][-1] > max_value :
max_value = data[i][-1]
rec = {i:data[i]}
c += 1
except EOFError:
if c == 0:
print("No records found")
else:
print("Employee with maximum salary:",rec)
break
[Link]()
maxSalary()
14. Write a user defined function named maxSalary() which displays the record[ name,
gender, dept, salary] of the employee with maximum salary from Sales department.
import pickle
def maxSalary() :
f = open("[Link]", 'rb')
c=0
max_value = 0
while True:
try:
data = [Link](f)
for i in data:
if data[i][2] == 'Sales':
Page 6 of 8
Class – XII Chapter – 5 (File Handling – Binary File Programs)Computer Science - 083
if c==0:
max_value = data[i][-1]
rec=data[i]
elif data[i][-1] > max_value:
max_value = data[i][-1]
rec=data[i]
c += 1
except EOFError:
if c == 0:
print("No records found")
else:
print("Employee with max salary from sales dept:",rec)
break
[Link]()
maxSalary()
15. Write a user defined function named minSalary() which display the record of
the employee with minmum salary.
import pickle
def minSalary() :
f = open("[Link]", 'rb')
c=0
min_value = 0
while True:
try:
data = [Link](f)
for i in data:
if c==0:
min_value=data[i][-1]
rec = i,data[i]
elif data[i][-1] < min_value:
min_value = data[i][-1]
rec = i,data[i]
c+=1
except EOFError:
if c == 0:
print("No records found")
else:
print("Employee with minimum salary:",rec)
break
[Link]()
minSalary ()
16. Write a user defined function named minSalary () which displays the record of
the employee with minimum salary from Purchase department.
import pickle
def minSalary () :
f = open("[Link]", 'rb')
c=0
min_value = 0
while True:
try:
data = [Link](f)
for i in data:
if data[i][2] == 'Purchase':
if c==0:
min_value = data[i][-1]
Page 7 of 8
Class – XII Chapter – 5 (File Handling – Binary File Programs)Computer Science - 083
rec = {i:data[i]}
elif data[i][-1] < min_value:
min_value = data[i][-1]
rec = {i:data[i]}
c += 1
except EOFError:
if c == 0:
print("No records found")
else:
print("Employee with min salary from Purchase department:")
for j in rec:
print("Empid : ",j)
print("Emp Name : ",rec[j][0])
print("Emp Gender : ",rec[j][1])
print("Emp Dept : ",rec[j][2])
print("Emp Salary : ",rec[j][3])
break
[Link]()
minSalary ()

Page 8 of 8

You might also like