Binary Files:
1. Binary files are made up of non-human readable characters and symbols, which require specific programs to
access its contents.
2. In Binary File, translation is not required because data is stored in bytes form.
3. Faster than text files.
4. pickle module is used for working with binary files
5. File extension will be .dat
6. There is no delimiter to end the file.
Working in Binary files:
pickle module: pickle module is used in binary file for load( ) and dump( ) methods which are used for
reading and writing into binary file respectively.
Pickling or Serialization: It is the process of converting python object into byte stream.
Pickling is done at the time of writing into a binary file.
Unpickling or De-serialization: It is the process of converting a byte stream into python object.
Unpickling is done at the time reading from a binary file
dump( ): is used to write data into binary file. load( ): it is used to read data from binary file.
Syntax: identifier = pickle.dump(data , file_pointer) Syntax: identifier = pickle.load(file_pointer)
Example: a= “My name is Anuj” Example: data = pickle.load(f) #Here ‘data’ is an
pickle.dump(a,f) #here ‘a’ contains data and ‘f’ is a identifier and ‘f’ is a file pointer.
file pointer.
File modes in Binary File
Mode Description Behavior
rb Read-only in binary mode Opens the file for reading binary
data. File must exist; otherwise, it
raises an error.
rb + Read and write in binary mode. Opens the file for both reading and
writing binary data. File must
exist; otherwise, it raises an error.
wb Write in binary mode. Opens the file for writing binary
data. Creates a new file or
truncates the existing file.
wb + Write and read in binary mode. Write and read in binary mode.
ab Append in binary mode. Opens the file for appending
binary data. Creates a new file if it
doesn't exist.
ab+ Append and read in binary mode. Opens the file for appending and
reading binary data. Creates a new
file if it doesn't exist.
, Sample Program-1 (CBSE 2025)
import pickle
def Create():
f=open("PASSENGERS.dat","wb")
while True:
P=input("Enter the PNR")
PName=input("Enter the name")
BRDSTN=input("Enter the Bording Station")
DESTN=input("Enter the Destination Station")
FARE=float(input(“Enter the Fare”))
d=[P,PName,BRDSTN,DESTN,FARE]
pickle.dump(d,f)
ch=input("Do u want to add y/n")
if ch in "nN":
break
f.close()
def SearchDestn(D):
f=open("PASSENGERS.dat","rb")
rec=pickle.load(f)
try:
1. Binary files are made up of non-human readable characters and symbols, which require specific programs to
access its contents.
2. In Binary File, translation is not required because data is stored in bytes form.
3. Faster than text files.
4. pickle module is used for working with binary files
5. File extension will be .dat
6. There is no delimiter to end the file.
Working in Binary files:
pickle module: pickle module is used in binary file for load( ) and dump( ) methods which are used for
reading and writing into binary file respectively.
Pickling or Serialization: It is the process of converting python object into byte stream.
Pickling is done at the time of writing into a binary file.
Unpickling or De-serialization: It is the process of converting a byte stream into python object.
Unpickling is done at the time reading from a binary file
dump( ): is used to write data into binary file. load( ): it is used to read data from binary file.
Syntax: identifier = pickle.dump(data , file_pointer) Syntax: identifier = pickle.load(file_pointer)
Example: a= “My name is Anuj” Example: data = pickle.load(f) #Here ‘data’ is an
pickle.dump(a,f) #here ‘a’ contains data and ‘f’ is a identifier and ‘f’ is a file pointer.
file pointer.
File modes in Binary File
Mode Description Behavior
rb Read-only in binary mode Opens the file for reading binary
data. File must exist; otherwise, it
raises an error.
rb + Read and write in binary mode. Opens the file for both reading and
writing binary data. File must
exist; otherwise, it raises an error.
wb Write in binary mode. Opens the file for writing binary
data. Creates a new file or
truncates the existing file.
wb + Write and read in binary mode. Write and read in binary mode.
ab Append in binary mode. Opens the file for appending
binary data. Creates a new file if it
doesn't exist.
ab+ Append and read in binary mode. Opens the file for appending and
reading binary data. Creates a new
file if it doesn't exist.
, Sample Program-1 (CBSE 2025)
import pickle
def Create():
f=open("PASSENGERS.dat","wb")
while True:
P=input("Enter the PNR")
PName=input("Enter the name")
BRDSTN=input("Enter the Bording Station")
DESTN=input("Enter the Destination Station")
FARE=float(input(“Enter the Fare”))
d=[P,PName,BRDSTN,DESTN,FARE]
pickle.dump(d,f)
ch=input("Do u want to add y/n")
if ch in "nN":
break
f.close()
def SearchDestn(D):
f=open("PASSENGERS.dat","rb")
rec=pickle.load(f)
try: