Binary Files
Binary Files
backslash from top most directory to all nested sub-directories followed by file name or
directory name.
Absolute path is a path-name which start from top most level of directory structure
while relative path is related to current working directory.
So full path name is the absolute path which is denoted as mentioned .
Relative pathname shows the two dots for current working directory followed by back slash
and file-name.
In os.path module two functions abspath() and relpath() function is used to show the
absolute and relative path of any file.
Here is example to show this.
>>> import os
>>>os.path.relpath('C:\\Users\\Dell\\AppData\\Local\\Programs\\Python\\P ython37-32\\
Programs', 'example1.txt') '..\\Programs'
BINARY FILES
Comparison in between Binary Files and Text Files
Text files: In this type of file, Each line of text is terminated with Binary files: In this type of file, there is no terminator for a
a special character called EOL (End of Line), which is the new line and the data is stored after converting it into machine
line character (‘\n’) in python by default. understandable binary language.
Text files Binary files
Human Readable Format Machine Readable Format
Data stored in text format Data stored in machine language
Each line of text is terminated with a special
character called EOL (End of Line), which is the There is no terminator for a line
new line character (‘\n’) in python by default.
Slow files due to the Internal Translation from HLL Fast because data is stored in Machine Language
to LLL and no translation
Used for storing text only Used for storing Python objects
No specific Module required “Pickle” module is required
Binary files: In this type of file, there is no
terminator for a line and the data is stored after
converting it into machine understandable binary
language.
• Binary files used to read and write text, images or
audio and video file formats.
• Binary files read and write the data in the form of
bytes.
• Binary files are not in human readable format as
information in binary code.
• Many operation like search, update, insertion and
deletion of record can be done in binary file
• Used for writing and reading python objects like
list, tuples, dictionaries, class etc.
Binary files can be open using
1. open() method
2.‘with’ statement.
Various modes of binary files are ‘rb’, ’wb’, ’ab’, ’rb+’, ’wb+’ and ‘ab+’
PICKLE MODULE
Pickling -Also called serializing process of convert Python objects like class, list, tuple, dictionary into binary data in the
forms of bytes
To write in a file we use dump()
So data associated to these objects write into binary file using the pickling. Pickling is done using dump()
method of pickle module
pickle.dump(object, file-object)
Unpickling- It is the reverse process of pickling or conversion of byte stream into a Python objects
To read in a file we use load()
when the objects are stored into binary file, we can read it by converting byte stream back into python objects this
process is known as unpickle. Unpickling is done by using the load() functions of pickle module.
object=pickle.load(file-object)
The load() function reads byte streams from binary file convert and return the object.
import pickle
dct={}
fbw=open("E:\\python files\\fd.dat","wb")
ans='y'
while ans=='y':
roll = int(input("enter roll number"))
name = input("enter name")
mks = float(input("enter marks"))
dct['roll']=roll
dct['name']=name
dct['mks']=mks
pickle.dump(dct,fbw)
ans=input("enter more records")
fbw.close()
Reading from A BINARY FILE
import pickle
dct={}
fbr=open("E:\\python files\\fd.dat","rb")
try:
while True:
dct=pickle.load(fbr)
print(dct)
except EOFError:
fbr.close()
WRITING, READING, SEARCH
&
UPDATION
IN
BINARY DATA FILES
#program to write data in CSV files
import csv
fh=open("E:\\python files\\student.csv","w")
studwriter=csv.writer(fh)
studwriter.writerow(['Roll NO','NAME','MARKS’])
for i in range(5):
print("Students record")
rno=int(input("Enter roll no."))
name=input("enter name")
mk=float(input("Enter marks"))
sturec=[rno,name,mk]
studwriter.writerow(sturec)
fh.close()
DAYTA WRITING IN FILE
MEMORY (RAM)
List:-record
List variable data 0 1 2
roll name marks BINARY FILE
STUD.DAT
STORED AT HDD
#Data reading from file
import pickle
frb=open("E:\python files\stud.dat","rb")
record=pickle.load(frb)
print("contents of records are")
try:
for i in record:
roll_no=i[0]
name=i[1]
marks=i[2]
print(roll_no, name,marks)
except EOFError:
frb.close()
DATA READING FROM FILE
MEMORY (RAM)
List:-record
0 1 2
BINARY FILE
roll name marks 10 ROHAN 585 STUD.DAT
12 ARHAM 582
#Search operation in File
import pickle
frb=open("E:\python files\stud.dat","rb")
studrec=pickle.load(frb)
found=0
rno=int(input("enter the roll no. to be search:"))
for i in studrec:
if i[0]==rno:
print("Record found in database")
found=1
break
if found==0:
print("record not found")
frb.close()
SEARCH OPERATION IN DATA FILE
MEMORY (RAM)
List:-record
0 1 2 BINARY FILE
STUD.DAT
10 ROHAN 585
Variableroll STORED AT HDD
11 AMIT 579
12 ARHAM 582
#Updation of records in file
import pickle
frb=open("E:\python files\stud.dat","rb+")
studrec=pickle.load(frb)
found=0
rno=int(input("enter the roll no. to be search:"))
for i in studrec:
if i[0]==rno:
print("Record found in database")
print("current name is :",i[1])
i[1]=input("new name:")
found=1
break
if found==1:
frb.seek(0)
pickle.dump(studrec,frb)
print("Name updated!!!")
frb.close()
UPDATION IN DATA FILE
MEMORY (RAM)
List:-record
BINARY FILE
0 1 2
i [0] i [1] i [2] STUD.DAT
10 ROHAN 585
Variableroll STORED AT HDD
11 amrita 579
12 ARHAM 582
Programs for practice
5. WAP to create a menu driven program which perform Insert, search, show and update operations
by using dictionary.