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
Storing data in a binary file:
# 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)
print(f.tell())
f.close()
Retrieval of data from a binary file:
# 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()
Searching a record in a binary file
#Write a python Program to search a record from the binary file “employee.dat” on the basis of employee number
import pickle
f=open("emp.data","rb")
#stu_rec=pickle.load(f)
found=0
eno=int(input("Enter the Employee Number to search"))
while True:
try:
obj=pickle.load(f)
if obj.id==eno:
found=1
obj.display()
except EOFError:
break
if found==0:
print("Data Not Found")
f.close()
Random Access of file:
Till this point we only looked into sequential access of file. Python allow random access of the data also using two inbuilt methods - seek() and tell().
seek():seek() function is used to change the position of the file handle (file pointer) to a given specific position in file. File pointer is like a cursor, which defines from where the data has to be read or written in the file. Method seek() sets the file’s current position at the offset.
Syntax for seek() is-
f.seek(file_location) #where f is the file pointer
Example:
f.seek(20) #This statement shall move the file pointer to 20th byte in the file
f.seek(offset, from_what) #where from_what specify the location i.e. start=0, current location=1, end of file=2
Example:(ONLY SUPPORTED IN BINARY FILES)
f.seek(–10,1) from current position, move 10 bytes backward
f.seek(10,1) from current position, move 10 bytes forward
f.seek(–20,1) from current position, move 20 bytes backward
f.seek(10,0) from beginning of file, move 10 bytes forward
tell(): tell() returns the current position of the file read/write pointer within the file.
Its syntax is:
f.tell() #where f is file pointer
Note:
- When we open a file in reading/writing mode, the file pointer rests at 0th byte
- When we open a file in append mode, the file pointer rests at the last byte.
# Write a Program to read byte by byte from a file using seek() and tell().
f=open("new.txt","r")
print("File pointer Before reading: ", f.tell())
s=f.read()
print("File pointer After reading: ", f.tell())
f.seek(0) #brings the pointer to the begining of the file
print("the current location of the file pointer: ",f.tell())
s=f.read(5)
print("First 5 bytes are: ",s)
print("the current location of the file pointer: ",f.tell())
f.seek(13,0)
s=f.read(6)
print("Next 5 bytes which is 5 bytes away from the current location: ",s)
print("the current location of the file pointer: ",f.tell())
Updating a record in a binary file:
Write a python Program to search a record from the binary file “employee.dat” on the basis of employee number and update the employee name¶
import pickle
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","r+b")
updated=0
eno=int(input("Enter the Employee Number to search"))
while True:
try:
current_pos=f.tell()
obj=pickle.load(f)
if obj.id==eno:
print(current_pos)
name=input("Enter the new name")
id1=obj.id
sal=obj.sal
e=emp(id1,name,sal)
f.seek(current_pos)
pickle.dump(e,f)
print(f.tell())
updated=1
break
except EOFError:
break
if updated==0:
print("Data Not Found")
else:
print("update successful")
f.close()
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>
2. Manish Kumar of class 12 is writing a program to create a CSV file “marks.csv” which will contain Student Name and total Marks for some entries. He has written the following code. As a programmer, help him to successfully execute the given task. KVS Preboard 2020-21 import _____________ # Line 1 def addCsvFile(Name,Marks): # to write / add data into the CSV file f=open(' user.csv','________') # Line 2 newFileWriter = csv.writer(f) newFileWriter.writerow([Name,Marks]) f.close() #csv file reading code def def readCsvFile(): # to read data from CSV file with open(' mark.csv','r') as newFile: newFileReader = csv._________(newFile) # Line 3 for row in newFileReader: print (row[0],row[1]) newFile.______________ # Line 4 addCsvFile(“Arjun”,”96”) addCsvFile(“amita”,”78”) readCsvFile() #Line 5 (a) Name the module he should import in (Line 1). (b) In which mode, Manish should open the file to add data into the file (Line 2) (c) Fill in the blank in Line 3 to read the data from a csv file. (d) Fill in the blank in Line 4 to close the file. (e) Write the output he will obtain while executing Line 5.
3.Write a code to count the total occurrences of the word “The” in a text file “biography.txt” KVS preboard 2020-21
4.A binary file “item.dat” has structure [itemNo, item_Name,brand,Price]. KVS Preboard
a.Write a user defined function CreateFile() to input data for a record and add to "item.dat".
b.Write a function CountRec(item_name) in Python which accepts the item name as parameter and count and return number of items by the given brand are stored in the binary file “Book.dat”
5.A binary file “STUDENT.DAT” has structure (admission number, Name, Percentage). Write a function countrec() in Python that would read contents of the file “STUDENT.DAT” and display the details of those students whose percentage is above 60. Also display number of students scoring above 60%
References:
- Core python programming - Dr. R. Nageswara Rao