BINARY FILE
Binary files :- store data in the binary format (0’s and 1’s) which is understandable by
the machine. So when we open the binary file it converts and displays data in a human-
readable format.
Write data to a Binary File:
To read the data from a binary file, we have to use load( ) function of pickle module.
Reading data to a Binary File:
To read the data from a binary file, we have to use load( ) function of pickle module.
PICKLING AND UNPICKLING
Pickling: Pickling is the process whereby a Python object is converted into a byte
stream.
Unpickling: A byte stream is converted into Python object
tell( ) and seek( ) methods:
tell( ): It returns the current position of cursor in file.
seek(offset) : Change the cursor position by bytes as specified by the offset.
A binary file [Link] [ roll , name , marks] . Define
get ( ):- To input values of roll number , name and marks and store it
into file [Link]
show( ):- To display content of file [Link]
import pickle
def get( ):
f=open('[Link]' , 'wb')
a=int(input('roll'))
b=input('name')
c=int(input('marks'))
z=[a,b,c]
[Link](z,f)
[Link]( )
def show( ):
f=open('[Link]' , 'rb')
try:
while True:
z=[Link](f)
print(z)
except:
[Link]( )
get ( )
show ( )
A Binary file [Link] contains[roll , name , marks ] Define the following
writestudent ( ):-To input values of roll number , name , marks and
store it in file [Link]
display ( ):- To display records of those students where marks are more
then 75
import pickle
def writestudent( ):
f=open('[Link]' , 'ab')
a=int(input('roll'))
b=input('name')
c=int(input('marks'))
z=[a,b,c]
[Link](z,f)
[Link]( )
def display( ):
f=open('[Link]' , 'rb')
try:
while True:
z=[Link](f)
if z[2] > 75 :
print(z)
except:
[Link]( )
writestudent( )
display( )
A binary file ' [Link] contains [name , subject , marks] define following
below mentioned functions
accept( ):- To add record into binary file '[Link]'
display ( ):- To print name and marks of those students whose subject
is computer
import pickle
def accept( ):
f=open('[Link]' , 'ab' )
a=input('name')
b=input('subject')
c=input('marks')
z=[ a,b,c ]
[Link]( z , f )
[Link]( )
def display( ):
f=open('[Link]' , 'rb')
try:
while True:
z=[Link](f)
if z[1]=='computer':
print(z[0] , z[1])
except:
[Link]( )
accept( )
display( )
A binary file ' [Link] ' contains [roll , name , marks] Define functions
add ( ):- To add records into '[Link]'
copy( ):- To copy those records into '[Link]' where marks is more then
50 and also count number of records copied
import pickle
def add( ):
f=open('[Link]' , 'ab')
a=int(input('roll'))
b=input('name')
c=int(input('marks'))
z=[a,b,c]
[Link](z,f)
[Link]( )
def copy( ):
f=open('[Link]' , 'rb')
f2=open('[Link]' , 'ab')
c=0
try:
while True:
z=[Link](f)
print(z)
if z[2] > 50 :
[Link](z,f2)
c=c+1
except:
[Link]( )
[Link]( )
print(c)
add( )
copy( )
A binary file ' [Link] ' contains [Book-ID , book name, price] define a
function
add( ):- to add record into Binary file [Link]
search( ):- To display details of those books that matches with the
value passed as parameter into function
import pickle
def add( ):
f=open('[Link]' , 'ab')
a=int(input('Book-ID'))
b=input('Book name')
c=int(input('price'))
z=[a,b,c]
[Link](z,f)
[Link]( )
def search( n):
f=open('[Link]', 'rb')
try:
while True:
z=[Link](f)
if z[0]==n :
print(z)
except:
[Link]( )
add( )
n=int (input('Book-ID'))
search(n )