Binary Files
Binary Files
Introduction
• A binary file is a computer file that is not a text
file.
• The term "binary file" is often used as a term
meaning "non-text file“
• A binary file is computer-readable but not
human-readable.
• A binary file is a file stored in binary format.
Use of Binary Files
import pickle
import pickle
student = [[1,”reena”,56],[2,”teena”,76]]
Method :3(take entry from user and can add multiple records in a file)
import pickle
student = [name,roll]
Method :4(take entry from user and append in a list,i.e. add single record at a time
import pickle
name = input('Enter name :')
roll = int(input('Enter roll :'))
s = [name,roll]
student=[]
student.append(s)
file = open('student.dat', 'wb') mode ‘b’ is compulsory to
perform with binary files
pickle.dump(student,file)
file.close()
Enter name :reena
print('Binary file Generated...') Enter roll :10
Binary file Generated...
Read Data from Binary File 11
import pickle
file = open('student.dat', 'rb') ‘rb’ stand for reading
data = pickle.load(file) mode + binary file
print(data)
file.close()
Read Data from Binary File 12
import pickle
file = open('student.dat', 'rb') ‘rb’ stand for reading
mode + binary file
data = pickle.load(file)
r = pickle.load(file)
y = pickle.load(file)
print(data)
print(r)
print(y)
file.close()
Read Data from Binary File 13
import pickle
file = open('student.dat', 'rb') ‘rb’ stand for reading
mode + binary file
for i in range(3):
data = pickle.load(file)
print(data)
file.close()
OR
import pickle
file = open('student.dat', 'rb')
for i in range(3):
for J in pickle.load(f):
print(J)
file.close()
Read Multiple Record from Binary File 14
import pickle
name = input('Enter name :')
roll = int(input('Enter roll :')) If records
student = [name,roll] are stored
file = open('student.dat', 'wb') in a list
pickle.dump(student,file) then to
file.close() display its
print('Binary file Generated...') content
Append Data in Binary File using List Concept 16
Method :1
import pickle
name = input('Enter name :')
roll = int(input('Enter roll :'))
student=[name,roll]
file = open('student.dat', 'ab')
pickle.dump(student, file)
‘ab’ stand for
file.close()
appending mode
print('Binary file appended...') in binary file
import pickle
N=int(input(“enter how many records u want to enter”))
file = open('student.dat', 'ab')
for i in range(N):
name = input('Enter name :')
roll = int(input('Enter roll :'))
student=[name,roll]
‘ab’ stand for
pickle.dump(student, file) appending mode
file.close() in binary file
print('Binary file appended...')
import pickle
file = open('student.dat', 'ab')
while True: ‘ab’ stand for
name = input('Enter name :') appending mode
roll = int(input('Enter roll :')) in binary file
student=[name,roll]
pickle.dump(student, file)
ch=input(“More records")
if(ch=='n' or ch=='N'): Enter name :sheena
break Enter roll :20
file.close() More records y
print('Binary file appended...') Enter name :heena
Enter roll :10
More records n
Binary file appended...
Search Data in Binary File 19
import pickle
n = input('Enter name to search :')
file1 = open('student.dat', 'rb')
while True:
try:
data = pickle.load(file1)
if data[0]==n:
print(data)
except:
break
file1.close()
Modify/Update a Record 20
Method :4
To modify the record
x=False
f=open("aa.dat","rb")
f1=open("temp.dat","wb")
dispr()
a=int(input("enter product no which we want to update:"))
while True:
try:
s=pickle.load(f)
if a==s[0]:
print("current name",s[1])
s[1]=input("enter name")
s[2]=float(input("enter price"))
pickle.dump(s,f1)
x=True
else:
pickle.dump(s,f1)
except:
break
if(x==False):
print("record not found")
f.close()
f1.close()
os.remove("aa.dat")
os.rename("temp.dat","aa.dat")
f2=open("aa.dat","rb")
while True:
try:
s=pickle.load(f)
print(s)
except:
break
f2.close()
Delete a Record 24
import pickle
import os
while True:
try:
data = pickle.load(file1)
if data[1]!= n: Enter name to delete :reena
pickle.dump(data,file2) Record deleted...
except:
break
file1.close()
file2.close()
os.remove('student.dat')
os.rename('temp.dat', 'student.dat')
print('Record deleted...')
Create a Binary File using Dictionary concept 26
import pickle
import pickle
file = open('student.dat', 'rb') ‘rb’ stand for reading
data = pickle.load(file) mode + binary file
print(data)
file.close()
import pickle
name = input('Enter name :')
roll = int(input('Enter roll :'))
student = {'name': name, 'roll': roll}
file = open('student.dat', 'wb')
{'name': 'reena', 'roll': '10'}
pickle.dump(student,file)
file.close()
print('Binary file Generated...')
Append Data in Binary File using Dictionary concept 28
import pickle
name = input('Enter name :')
roll = int(input('Enter roll :'))
student = {'name': name, 'roll': roll}
file = open('student.dat', 'ab')
pickle.dump(student, file)
‘ab’ stand for
file.close() appending mode
print('Binary file appended...') in binary file
import pickle
n = input('Enter name to search :')
file1 = open('student.dat', 'rb')
while True:
try:
data = pickle.load(file1)
if n == data['name']:
print(data)
except:
break
file1.close()
Update Record using Dictionary Concept 30
import pickle
import os
import pickle
import os
while True:
try:
data = pickle.load(file1)
if n != data['name']: Enter name to delete :reena
pickle.dump(data,file2) Record deleted...
except:
break
file1.close()
file2.close()
os.remove('student.dat')
os.rename('temp.dat', 'student.dat')
print('Record deleted...')