Operations on Binary Files
1. To create and store/write data in
a binary file
import pickle
L=[ ]
file=open("stu.txt","wb")
size=0
size=int(input("How many Records:"))
for i in range(0,size,1):
name=input("Enter Name of student :")
roll=int(input("Enter Roll of student :"))
L=[name,roll]
pickle.dump(L,file)
print(size," Records are written in the file.")
file.close( )
2. To read a binary file
import pickle
L=[]
file=open("stu.txt","rb")
size=0
while True:
try:
L=pickle.load(file)
name=L[0]
print("Name of student is",name)
roll=L[1]
print("Roll of student is",roll)
size=size+1
except EOFError:
break
print(" Total no. of Records :",size)
file.close( )
3. To read and search a particular record in
a binary file
import pickle
L=[]
file=open("stu.txt","rb")
flag=0
R=int(input("Enter roll to search :"))
while True:
try:
L=pickle.load(file)
roll=L[1]
if R==roll:
flag=1
print("Record found successfully.\n")
name=L[0]
print("Name of student is: ",name)
print("Roll of student is: ",roll)
except EOFError:
break
if flag==0:
print(" Record is not found.")
file.close()
4. To insert / append data in a
binary file
import pickle
L=[]
s=0
file=open("stu.txt","ab")
while True:
print("Enter data:\n")
name=input("Enter Name of student :")
roll=int(input("Enter Roll of student :"))
L=[name,roll]
pickle.dump(L,file)
s=s+1
print("The Record is appended.\n")
choice=input("Do you want to add more Record?")
if choice=='y' or choice=='y':
continue
else:
break
print("Total",s," Record(s) are appended.")
file.close()
5. To update/modify data in a
binary file
import pickle
L=[]
file=open("stu.txt","r+b")
flag=0
R=int(input("Enter Roll to update details :"))
while True:
try:
L=pickle.load(file)
roll=L[1]
if R==roll:
flag=1
print("Record found successfully.\n")
name=L[0]
print("Name of student is: ",name)
print("Roll of student is: ",roll)
print("Enter new details of student:\n”)
L[0]=input("Enter new Name of the student:")
L[1]=int(input("Enter new Roll of the student:"))
file.seek(-len(L),1)
pickle.dump(L,file)
print("Record has been modified successfully.\n")
break
except EOFError:
break
if flag==0:
print(" Record is not found.")
file.close()
6. To copy data from one binary file
to another file.
#Program to copy Records of those
students whose name begin with 'A‘ to
an another file
import pickle
L=[]
file1=open("stu.txt","rb")
file2=open("backup.txt","wb")
name=" "
flag,c=0,0
while True:
try:
L=pickle.load(file1)
name=L[0]
if name[0]=='A':
pickle.dump(L,file2)
flag=1
c=c+1
except EOFError:
break
if flag==0:
print(" No such Record is found or copied.")
elif flag==1:
print(c,"Record(s) found and successfully copied.\n")
file2.close()
file3=open("backup.txt","rb")
size=0
print("Contents of the copied file are:\n“)
while True:
try:
L=pickle.load(file3)
name=L[0]
print("Name of student is",name)
roll=L[1]
print("Roll of student is",roll)
print("\n")
size=size+1
except EOFError:
break
print(" Total no. of Record(s) :",size)
file3.close()
file1.close()
7. To remove/delete data from in a
binary file
import pickle
import os
L=[]
file1=open("stu.txt","rb")
file2=open("temp.txt","wb")
flag=0
R=int(input("Enter roll to delete a record :"))
while True:
try:
L=pickle.load(file1)
roll=L[1]
if R!=roll:
pickle.dump(L,file2)
else:
flag=1
except EOFError:
break
if flag==0:
print(" Record is not found.")
print(" It can not be deleted.")
file1.close()
file2.close()
elif flag==1:
print(" Record is found and successfully deleted.")
file1.close()
file2.close()
os.remove("stu.txt")
os.rename("temp.txt","stu.txt")
file3=open("stu.txt","rb")
size=0
print("New Contents of the file are:\n")
while True:
try:
L=pickle.load(file3)
name=L[0]
print("Name of student is",name)
roll=L[1]
print("Roll of student is",roll)
print("\n")
size=size+1
except EOFError:
break
print(" Total no. of Record(s) :",size)
file3.close()
FOR REVISION WORK
8. A binary file “Stu.dat” stores
Student’s Admission number , name ,
average marks , stream and grade .
Write a function to update average
marks of all students by 10% who get
average marks less than 33 and their
stream is Humanities . Display the
details of all students with updated
values.
def Update( ):
L=[ ]
file=open("Stu.dat","r+b")
c=0
flag=0
while True:
try:
L=pickle.load(file)
if L[2]<33 and L[3]=="Humanities":
flag=1
L[2]=L[2]+0.1*L[2]
file.seek(-len(L),1)
pickle.dump(L,file)
c=c+1
except EOFError:
break
if flag==0:
print(" No such Record is found.")
elif:
print(c,"Record(s) updated successfully.\n")
file.close()
print("Updated Details of students :")
file1=open("Stu.dat","rb")
while True:
try:
L=pickle.load(file1)
print("Admission No. of student is",L[0])
print("Name of student is",L[1])
print("Average Marks of student are",L[2])
print("Stream of student is",L[3])
print("Grade of student is",L[4])
print("\n")
except EOFError:
break
file1.close( )