0% found this document useful (0 votes)
14 views

Binary Files

cs

Uploaded by

Neetu Prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Binary Files

cs

Uploaded by

Neetu Prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

BINARY FILE HANDLING

Open and closing a binary file

Example 1:
f = open("teams.dat", "rb")
This statement opens the binary file in read mode.

Example 2:
f = open("teams.dat", "wb")
This statement opens the binary file in write mode.

Example 3:
f = open("teams.dat", "ab")
This statement opens the binary file in append mode.

f.close()

Reading and Writing Binary Data

f=open("abc","wb")

l=[10,20,30]

a=bytes(l)

f.write(a)

f.close()

f=open("abc","rb")

print(list(f.read()))

f.close()

PICKLE MODULE
“Pickling” is the process in which a Python object is converted into a byte stream,
and “unpickling” is the inverse operation, whereby a byte stream is converted
back into an object.

Python's pickle module is used for pickling and unpickling .

dump method is used for pickling

load method is used for unpickling.

Example program to add a list to binary file using pickle module:

import pickle
f = open("abc.data","wb")
L = [10,20,30]
pickle.dump(L,f)
f.close()
f = open("abc.data","rb")
y= pickle.load(f)
print(y)
f.close()

SEARCH OPERATION ON A BINARY FILE


#Create a binary file with name and roll number.
#Search for a given roll number and display the name,
#if not found display appropriate message.
#STEP-1 Create a binary file with name and roll number
import pickle
L=[]
f=open("students.dat","wb")
while True:
rno=int(input("Enter roll no:"))
name=input("Enter name:")
L=[rno,name] #INDEX 0 IS FOR ROLL NO
#INDEX 1 IS FOR NAME
pickle.dump(L,f)
ch=input("press y to continue")
if ch!='y':
break

f.close()

#STEP-2 Search for a given roll number & display the name

f=open("students.dat","rb")
rno=int(input("enter roll number to search"))
found=False

try:
while True:
rec = pickle.load(f)
if rec[0]==rno:
print("Roll No.",item[0],"Name:",item[1])
found=True
break
except:
f.close()
if not found:
print(rno,"not found in file")

A binary file “Book.dat” has structure [BookNo, Book_Name, Author, Price].


i. Write a user defined function CreateFile() to input data for a record and add to
Book.dat .
ii. Write a function CountRec(Author) in Python which accepts the Author name as
parameter and count and return number of books by the given Author are stored in
the binary file “Book.dat”

import pickle
def CreateFile():
f=open("Book.dat","ab")
BookNo=int(input("Enter Book No:"))
Book_Name=input("Enter Book Name:")
Author=input("Enter Author Name:")
Price=float(input("Enter Price:"))
rec=[BookNo, Book_Name, Author, Price]
pickle.dump(rec,f)
f.close()

def CountRec(Author):
f=open("Book.dat","rb")
count=0
try:
while True:
rec=pickle.load(f)
if rec[2]==Author:
count+=1
except:
f.close()
return count
UPDATE OPERATION ON A BINARY FILE

Algorithm
1.Read the data to be update bookno,price
2. Read data from file rec by rec and append to List
update price of rec containing book no
3.Open file write mode and write the above list into
file.
4.Read and display file contents

Program

#1.Read the data to be update bookno,price

bookno=int(input("enter book no:"))


price=float(input("enter price:"))

#2. Read data from file rec by rec and append to List
# update price of rec containing book no

f=open("Book.dat","rb")
L=[]
import pickle
try:
while True:
rec=pickle.load(f)
if rec[0]==bookno:
rec[3]=price
L.append(rec)

except:
f.close()
print(L)
#3.Open file write mode and write the above list into
file.

f=open("Book.dat","wb")
for x in L:
pickle.dump(x,f)
f.close()

#4.Read and display file contents


f=open("Book.dat","rb")
print("after update")
try:
while True:
rec=pickle.load(f)
print(rec[0],rec[1],rec[2],rec[3])
except:
f.close()

You might also like