0% found this document useful (0 votes)
37 views5 pages

## Program To Create/Write Data To A Binary File

The document contains multiple Python programs that demonstrate how to create, read, write, search, copy, and delete records in a binary file using the pickle module. It includes functionalities for adding employee records, displaying them, searching for specific records, copying records based on salary criteria, and deleting records. Each program is structured to handle exceptions and manage file operations effectively.

Uploaded by

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

## Program To Create/Write Data To A Binary File

The document contains multiple Python programs that demonstrate how to create, read, write, search, copy, and delete records in a binary file using the pickle module. It includes functionalities for adding employee records, displaying them, searching for specific records, copying records based on salary criteria, and deleting records. Each program is structured to handle exceptions and manage file operations effectively.

Uploaded by

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

## PROGRAM TO CREATE/WRITE DATA TO A BINARY FILE

import pickle
f=open("emp.pickle","ab")
while True:
empid=int(input("ENTER EMPLOYEE ID:- "))
name=raw_input("ENTER EMPLOYEE NAME:- ")
desig=raw_input("ENTER EMPLOYEE DESIGNATION:- ")
sal=float(input("ENTER EMPLOYEE SALARY:- "))
dept=raw_input("ENTER EMPLOYEE DEPARTMENT:-")
l=[empid,name,desig,sal,dept]
pickle.dump(l,f)
choice=raw_input("DO YOU WANT TO ADD MORE RECORDS (PRESS y or n)")
if choice=='n'or choice=='N':
print("THANKS FOR ENTERING RECORD(S)")
break
f.close()

## PROGRAM TO READ DATA FROM A BINARY FILE


import pickle
f=open("emp.pickle","rb")

try:
while True:
l=pickle.load(f)
print l

except EOFError:
print("ALL RECORDS READ FROM FILE" )
f.close()

## PROGRAM TO READ DATA FROM A BINARY FILE DISPLAY RECORDWISE


import pickle
f=open("emp.pickle","rb")

try:
while True:
l=pickle.load(f)
print "==============================================="
print "EMPLOYEE ID:-",l[0]
print "EMPLOYEE NAME:-",l[1]
print "EMPLOYEE DESINATION:-",l[2]
print "EMPLOYEE SALARY:-",l[3]
print "EMPLOYEE DEPARTMENT:-",l[4]
print "==============================================="

except EOFError:
print("ALL RECORDS READ FROM FILE" )
f.close()

## PROGRAM TO SEARCH FOR A GIVEN RECORD FROM A BINARY FILE AND


##DISPLAY IT
import pickle
f=open("emp.pickle","rb")
found=0
eid=int(input("enter the employee id of the employee you are looking for"))
try:
while True:
l=pickle.load(f)
if eid==l[0]:
found=1
print "==============================================="
print "EMPLOYEE ID:-",l[0]
print "EMPLOYEE NAME:-",l[1]
print "EMPLOYEE DESINATION:-",l[2]
print "EMPLOYEE SALARY:-",l[3]
print "EMPLOYEE DEPARTMENT:-",l[4]
print "==============================================="
break

except EOFError:
print("ALL RECORDS READ FROM FILE \n\nsorry " )
if found==0:
print "\nRECORD NOT FOUND"

f.close()

#PROGRAM TO COPY THE CONTENT OF ONE FILE INTO ANOTHER.


import pickle
f=open("emp.pickle","rb")
nf=open("empcopy.pickle","wb")
try:
while True:
l=pickle.load(f)
pickle.dump(l,nf)

except EOFError:
print("ALL RECORDS COPIED TO empcopy.pickle FILE " )

f.close()
nf.close()

#PROGRAM TO COPY SELECTED RECORDS FROM ONE FILE TO ANOTHER FILE


SUPPOSE ##WHOSE SALARY IS MORE THAN 50000.
CONSIDERINGTHE STRUCTURE OF THE RECORD BE LIST EMP [EMPID, EMPNAME,
EMPDESIG, EMPSAL, EMPDEPT).
import pickle
f=open("emp.pickle","rb")
nf=open("sal.pickle","wb")

try:
while True:
l=pickle.load(f)
if l[3]>50000:
pickle.dump(l,nf)

except EOFError:
print("ALL RECORDS who are having salary more than 50000 COPIED TO sal.pickle FILE " )

f.close()
nf.close()

#PROGRAM TO DELETE A RECORD FROM A BINARY FILE.

import pickle
import os
found=0
f=open("emp.pickle","rb")
nf=open("temp.pickle","wb")
eid=int(input("enter empid"))
try:
while True:
l=pickle.load(f)
if l[0]!=eid:
pickle.dump(l,nf)
else:
found=1
print("DETIALS OF RECORD DELETED")
print("===============================")
print "EMPID:-",l[0]
print "EMP NAME:-",l[1]
print "EMP DESIGNATION:-",l[2]
print "EMP SALARY:-",l[3]
print "EMP DEPARTMENT:-",l[4]

except EOFError:
if found==1:
print("=========RECORD DELETED=========")
else:
print("RECORD NOT FOUND")

f.close()
nf.close()
os.remove("emp.pickle")
os.rename("temp.pickle","emp.pickle")

You might also like