0% found this document useful (0 votes)
7 views7 pages

14 March Day

The document provides Python functions for handling binary files using the pickle module, including writing employee data to files in both list and dictionary formats, reading data, searching records by department number, updating salaries, and deleting records. It also includes tasks related to counting records and copying specific records from a product file. Each function is accompanied by example code demonstrating its functionality.

Uploaded by

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

14 March Day

The document provides Python functions for handling binary files using the pickle module, including writing employee data to files in both list and dictionary formats, reading data, searching records by department number, updating salaries, and deleting records. It also includes tasks related to counting records and copying specific records from a product file. Each function is accompanied by example code demonstrating its functionality.

Uploaded by

Dev Ashwanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Binary files

=> to store the block of data into binary files


pickle module=> two functions
1. load() => to read data from file
2. dump() => to write data into file

1. write a python function to read empno,ename,salary,deptno


from user and write into emp.dat file continuously
solution:

import pickle
def writebinary():
f1=open("emp.dat","wb")
while True:
empno=int(input("enter the empno"))
ename=input("enter the empname")
salary=int(input("enter the salary"))
deptno=int(input("enter the deptno"))
l1=[empno,ename,salary,deptno]
pickle.dump(l1,f1)
op=input("enter the option to continue or not")
if op=='n':
break
f1.close()
writebinary()

2. write data into emp.dat file in dicitionary format


{"empno":empno,"ename":ename,"salary":salary,"deptno":deptno}

solution:
import pickle
def writebinary():
d1={}
f1=open("empdict.dat","wb")
while True:
empno=int(input("enter the empno"))
ename=input("enter the empname")
salary=int(input("enter the salary"))
deptno=int(input("enter the deptno"))
d1["empno"]=empno
d1["ename"]=ename
d1["salary"]=salary
d1["deptno"]=deptno
pickle.dump(d1,f1)
op=input("enter the option to continue or not")
if op=='n':
break
f1.close()
writebinary()

3)write a python function readdata() to read data from


emp.dat file in the format of
[empno,ename,salary,deptno]

import pickle
def readdata():
f1=open("emp.dat","rb")
try:
while True:
l1=pickle.load(f1)
print(l1)

except EOFError:
f1.close()
readdata()

3)write a python function readdata() to read data from


emp.dat file in the format of
{"empno":empno,"ename":ename,"salary":salary,"deptno":deptno}

solution:
import pickle
def readdata():
f1=open("empdict.dat","rb")
try:
while True:
d1=pickle.load(f1)
print(d1)

except EOFError:
f1.close()
readdata()
"""
{'empno': 1001, 'ename': 'raja', 'salary': 50000, 'deptno': 10}
{'empno': 1002, 'ename': 'kartick', 'salary': 70000, 'deptno': 20}
{'empno': 1003, 'ename': 'saran', 'salary': 30000, 'deptno': 20}

"""

5)Write a python function to read from emp.dat file


and get deptno from user and display the records
in given deptno.
emp.dat str is [empno,ename,salary,deptno]
solution:

import pickle
def searchrecord():
f1=open("emp.dat","rb")
deptno=int(input("enter the search deptno"))
count=0
try:
while True:
l1=pickle.load(f1)
if l1[3]==deptno:
print("found",l1)
count=count+1
except EOFError:
f1.close()
if count==0:
print("deptno is not found")
searchrecord()
5)Write a python function to read from empdict.dat file
and get deptno from user and display the records
in given deptno.
emp.dat str is {"empno":empno,"ename":ename,"salary":salary,
"deptno":deptno}

solution:
import pickle
def searchrecord():
f1=open("empdict.dat","rb")
deptno=int(input("enter the search deptno"))
count=0
try:
while True:
d1=pickle.load(f1)
if d1["deptno"]==deptno:
print("found",d1)
count=count+1
except EOFError:
f1.close()
if count==0:
print("deptno is not found")
searchrecord()

update()

emp.dat file
[1001, 'raja', 60000, 10]
[1002, 'kartick', 90000, 20]
[10003, 'kavin', 78000, 20]
[1004, 'selvi', 20000, 10]
[1005, 'kala', 50000, 20]
7) write a python function to increment the salary 5000
to all employees.

solution:
import pickle
def updaterecord():
f1=open("emp.dat","rb+")

try:
while True:
pos=f1.tell()
l1=pickle.load(f1)
##if condition for particular record
l1[2]=l1[2]+5000
f1.seek(pos)
pickle.dump(l1,f1)

except EOFError:
f1.close()
updaterecord()

def readdata():
f1=open("emp.dat","rb")
try:
while True:
l1=pickle.load(f1)
print(l1)

except EOFError:
f1.close()
readdata()

8)Write apython function to delete a record from emp.dat


file for the given empno.
emp.dat [empno,ename,salary,deptno]
solution:
import pickle
import os
def deleterecord():
f1=open("emp.dat","rb")
f2=open("new.dat","wb")
empno=int(input("enter the empno"))
try:
while True:
l1=pickle.load(f1)
if l1[0]!=empno:
pickle.dump(l1,f2)
except EOFError:
f1.close()
f2.close()
os.remove("emp.dat")
os.rename("new.dat","emp.dat")
deleterecord()

def readdata():
f1=open("emp.dat","rb")
try:
while True:
l1=pickle.load(f1)
print(l1)

except EOFError:
f1.close()
readdata()
Tasks
1. consider the file product.dat containing records of the following
str
[prodid,prodname,price,category]
i)write a function countrec() to count the no of records in
product.dat file.

solution:
def countrec():
f1=open("product.dat","rb")
count=0
try:
while True:
f1=pickle.load(f1)
count=count+1

except EOFError:
f1.close()
print("no of records=",count)
countrec()
Tasks
1. consider the file product.dat containing records of the following
str
[prodid,prodname,price,category]

ii)a)Write a function copydata() that reads contents from


the file product.dat and copies those records category is
'Homeappliances" to another file named as new.dat file.
b)write a function display() to display the content from
new.dat file
pickle.load(f1)
1001 lg 888 home
for record in content

solution:
import pickle
def copydata():
f1=open("product.dat","rb")
f2=open("new.dat","wb")
try:
while True:
l1=pickle.load(f1)
if l1[3]=="Homeappliances":
pickle.dump(l1,f2)
except EOFError:
f1.close()
f2.close()
def displaydata():
f1=open("new.dat","rb")
try:
while True:
l1=pickle.load(f1)
print(l1)
except EOFError:
f1.close()
copydata()
displaydata()

You might also like