Binary files handle data in the form of bytes. Hence, they can be used to read or write text, images or audio and video files. To open a binary files for reading purpose, we can user 'rb' mode. Here, 'b' is attached to 'r' to represent that its is a binary files. Similarly to write bytes into a binary file, we can use 'wb' mode. To read bytes from a binary file, we can use the read() method and to write bytes into a binary file, we can use the write() method
Example of Binary Files:
- Document files: .pdf, .doc, .xls etc.
- Image files: .png, .jpg, .gif, .bmp etc.
- Video files: .mp4, .3gp, .mkv, .avi etc.
- Audio files: .mp3, .wav, .mka, .aac etc.
- Database files: .mdb, .accde, .frm, .sqlite etc.
- Archive files: .zip, .rar, .iso, .7z etc.
- Executable files: .exe, .dll, .class etc.
# A python program to copy an image file into another file
file1 = open("cat1.jpg","rb")
file2=open("cat2.jpg","wb")
data=file1.read()
file2.write(data)
file1.close()
file2.close()
Binary file opening modes: The mode in the open function syntax will tell Python as what operation you want to do on a file
- ‘wb’ – Open a file for write only mode in the binary format.
- ‘rb’ – Open a file for the read-only mode in the binary format.
- ‘ab’ – Open a file for appending only mode in the binary format.
- ‘rb+’ – Open a file for read and write only mode in the binary format.
- ‘ab+’ – Open a file for appending and read-only mode in the binary format.
Pickle or serialization is a process of converting a class object into a byte stream so that it can be stored in a file. This method help in storing structured data ina file like employee details like employee identification number(int type), name(string type) and salary(float type) in a file
Two important function of pickle module:
- pickle.dump(object, file): This function is used to store the 'object' into the binary 'file'.pickling is done using the dump() function
- pickel.load(file): The load function reads an object from a binary 'file' and returns it into 'object'.unpickling is done with the help of load() function
# A python program to store the data of employee(id, name, salary) in a binary file
import pickle
# employee class
class emp:
def __init__(self, id,name,sal):
self.id=id
self.name=name
self.sal=sal
def display(self):
print(self.id," ",self.name," ",self.sal)
f=open("emp.data",'wb')
n=int(input("Enter the number of employee"))
for i in range(n):
id=int(input("Enter the id: "))
name=input("Enter employee name: ")
sal=float(input("Enter employee salary"))
e=emp(id,name,sal)
pickle.dump(e,f)
f.close()
# Python program to read the data(unpickle) of emp file and display it
import pickle
f=open("emp.data","rb")
print("Employee Details")
while True:
try:
obj=pickle.load(f)
obj.display()
except EOFError:
print('Thats it ......')
break
f.close()
- Write a python program to create a binary file and store the following records:
roll(int type)
name(string type)
percentage(float type)
grade(string type)
Note : take 4 value for the records from the user </p>
References:
- Core python programming - Dr. R. Nageswara Rao