You are on page 1of 12

Binary File – Practice Programs.

Master Program for binary file:

(reading)
File Name : employee.dat
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 display():
f = open("employee.dat", 'rb')
c = 0
while True:
try:
data = pickle.load(f)
print(data)
c = c + 1
except EOFError:
if c== 0:
print("No records found")
else:
print("Number of records:", c)
break
f.close()
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("employee.dat", 'rb')
c = 0
s = 0
while True:
try:
data = pickle.load(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
f.close()
totalsal()
3. Write a user defined function named avgsal() to display the Average salary of all the
employees

import pickle

def avgsal():
f = open("employee.dat", 'rb')
c = 0
s = 0
while True:
try:
data = pickle.load(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
f.close()
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("employee.dat", 'rb')
c = 0
s = 0
while True:
try:
data = pickle.load(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
f.close()
avgsalPurchase()
5. Write a user defined function named maleemployee() to display the names of all the
male employees.

import pickle

def maleemployee():
f = open("employee.dat", 'rb')
c = 0
s = 0
while True:
try:
data = pickle.load(f)
if data[2] == 'M':
print(data[1])
c+=1

except EOFError:
if c== 0:
print("No records found")
else:
print("Total Records:", c)
break
f.close()
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. (Multi data view and Compound
condition)

import pickle

def salary():
f = open("employee.dat", 'rb')
c = 0
s = 0
while True:
try:
data = pickle.load(f)

if data[2] == 'F' 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
f.close()
salary()
7. Write a user defined function named count_emp() to display the number of male and
female employees from the file. (Multi-parameter count)

import pickle

def count_emp():
f = open("employee.dat", 'rb')
male = 0
female = 0
while True:
try:
data = pickle.load(f)

if data[2] == 'M' :
male+=1
elif data[2] == 'F':
female+=1

except EOFError:
if male == 0 and female == 0:
print("No records found")
else:
print("No of Male Employees:", male)
print("No of Male Employees:", female)
break
f.close()
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("employee.dat", 'rb')
c = 0
while True:
try:
data = pickle.load(f)

if data[0] == empid :
print(data)
c += 1

except EOFError:
if c == 0:
print("No records found")

break
f.close()
search(1004)
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("employee.dat", 'rb')
c = 0
while True:
try:
data = pickle.load(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
f.close()
search(70000)

10. Write a user defined function named copyPurchase() which copies the records of
employees who belong to “Purchase” department to a new file named “Purchase.dat”

import pickle

def copyPurchase() :
f1 = open("employee.dat", 'rb')
f2 = open("Purchase.dat", 'wb')
c = 0
while True:
try:
data = pickle.load(f1)

if data[3] == 'Purchase' :
pickle.dump(data, f2)
c += 1

except EOFError:
if c == 0:
print("No records found")
else:
print("No of records:",c)

break
f1.close()
f2.close()

copyPurchase()
Dicitonary Structure
File Name : emp.dat
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 that matches the entered employee id from the file.

import pickle

def search() :
f = open("emp.dat", 'rb')
c = 0
empid = int(input("Enter the employee ID to be searched:"))
while True:
try:
data = pickle.load(f)
for i in data: #traverse on dictionary-i gets empid
if i == empid:
print(data)
c += 1
except EOFError:
if c == 0:
print("No records found")
else:
print("No of records:",c)

break
f.close()
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 search() :
f = open("emp.dat", 'rb')
c = 0
while True:
try:
data = pickle.load(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
f.close()
search()
13. Write a user defined function named maxSalary() which display the record of the
employee with maximum salary.

import pickle
def maxSalary() :
f = open("employee.dat", 'rb')
c = 0
max_value = 0
while True:
try:
data = pickle.load(f)
if c == 0: #setting max value to files first record salary
max_value = data[-1]

if data[-1] > max_value :


m = data[-1]
rec = data
c += 1
except EOFError:
if c == 0:
print("No records found")
else:
print("Employee with maximum salary:",rec)
break
f.close()
maxSalary()

14. Write a user defined function named maxSalary() which displays the record of the
employee with maximum salary from Sales department.

import pickle
def maxSalary() :
f = open("employee.dat", 'rb')
c = 0
max_value = 0
while True:
try:
data = pickle.load(f)
if data[3] == 'Sales':
if c==0: #setting max_value and rec with
max_salary to first record value
max_value = data[-1]
rec = data
elif data[-1] > max_value:
max_value = data[-1]
rec = data
c += 1
except EOFError:
if c == 0:
print("No records found")
else:
print("Employee with max salary from sales dept:",rec)

break
f.close()
maxSalary()
15. Write a user defined function named minSalary() which display the record of the
employee with maximum salary.

import pickle
def minSalary() :
f = open("employee.dat", 'rb')
c = 0
min_value = 0
while True:
try:
data = pickle.load(f)
if c == 0: #setting min value to files first record salary
min_value = data[-1]

if data[-1] < min_value:


min_value = data[-1]
rec = data
c += 1
except EOFError:
if c == 0:
print("No records found")
else:
print("Employee with minimum salary:",rec)
break
f.close()
minSalary ()

16. Write a user defined function named minSalary () which displays the record of the
employee with maximum salary from Purchase department.

import pickle
def minSalary () :
f = open("employee.dat", 'rb')
c = 0
min_value = 0
while True:
try:
data = pickle.load(f)
if data[3] == 'Purchase':
if c==0: #setting min _value and rec with
min_value = data[-1]
rec = data
elif data[-1] < min_value:
min_value = data[-1]
rec = data
c += 1
except EOFError:
if c == 0:
print("No records found")
else:
print("Employee with min salary from Purchase
department:",rec)

break
f.close()
minSalary ()
Writing data to files
File Name : employee.dat
Structure - [empid, name, gender, dept, salary]

1. Write a user defined function named writedata () to write n number records to the
file.

import pickle
def writedata():
f = open("employee.dat", 'ab')
n = int(input("Enter the number of records:"))
for i in range(n):
empid = int(input("Enter the Employee ID:"))
empname = input("Enter the Employee Name:")
gender = input("Enter the Gender:")
department = input("Enter the department:")
salary = int(input("Enter the salary:"))
data = [empid, empname, gender, department, salary] #for tuple
change [] to ()
pickle.dump(data, f)
f.close()
writedata()

2. Write a user defined function named writedata () to write a record to the file. The
data should be written only if the salary is greater than 50000 else it should display
“Salary is less, cannot Save”

import pickle
def writedata():
f = open("employee.dat", 'ab')

empid = int(input("Enter the Employee ID:"))


empname = input("Enter the Employee Name:")
gender = input("Enter the Gender:")
department = input("Enter the department:")
salary = int(input("Enter the salary:"))
if salary > 50000:
data = [empid, empname, gender, department, salary] #for tuple
change [] to ()
pickle.dump(data, f)
else:
print("Salary is less, cannot save")
f.close()
writedata()
Single Record Input:

3. Write a user defined function named writedata () to get the employee details as input
and add it to the binary file.

import pickle
def writedata():
f = open("employee.dat", 'ab')
empid = int(input("Enter the Employee ID:"))
empname = input("Enter the Employee Name:")
gender = input("Enter the Gender:")
department = input("Enter the department:")
salary = int(input("Enter the salary:"))
data = [empid, empname, gender, department, salary] #for tuple
change [] to ()
pickle.dump(data, f)
f.close()

writedata()

You might also like