[go: up one dir, main page]

0% found this document useful (0 votes)
21 views8 pages

BINARY

The document contains Python code for handling binary files using the pickle module. It includes functions for writing, reading, searching, and updating records for various data structures such as student records, plant details, book information, and movie types. Each section provides specific functionalities like adding records, displaying high-priced items, counting books by an author, and filtering movie types.

Uploaded by

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

BINARY

The document contains Python code for handling binary files using the pickle module. It includes functions for writing, reading, searching, and updating records for various data structures such as student records, plant details, book information, and movie types. Each section provides specific functionalities like adding records, displaying high-priced items, counting books by an author, and filtering movie types.

Uploaded by

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

BINARY FILES

import pickle

def Write():
f = open("BinaryData.dat", "wb")
while True:
rno = int(input("Roll No: "))
name = input("Name: ")
marks = int(input("Marks: "))
data = [rno, name, marks]
pickle.dump(data,f)
ch = input("Do you want to add more records?
(y/n): ")
if ch in 'nN':
break
f.close()

def Read():
f = open("BinaryData.dat", "rb")
try:
while True:
data = pickle.load(f)
print(data)
except EOFError:
f.close()
def Search():
f = open("BinaryData.dat", "rb")
r = int(input("Enter roll number whose record is
to be searched: "))
try:
while True:
data = pickle.load(f)
if data[0] == r:
print(data)
except EOFError:
f.close()

def Update():
f = open("BinaryData.dat", "rb")
r = int(input("Enter roll number whose marks is to
be updated: "))
L =[]
try:
while True:
data = pickle.load(f)
if data[0] == r:
data[2] = int(input("Enter updated
marks: "))
L.append(data)
except EOFError:
f.close()
f = open("BinaryData.dat", "wb")
pickle.dump(L, f)
f.close()

Write()
Read()
Search()
Update()

1. A binary file “Plants.dat” has a structure [ID, Name, Price]

- Write the definition of a function WRITEREC() in Python , to


input data for records from user and write them to file Plants.dat
- Write the definition of a function SHOWHIGH() in python , which
reads the records of Plants.dat and displays those records for
which the price is more than 500.

import pickle

def WRITEREC():
f = open("Plants.dat", "wb")
while True:
ID = int(input("Plant Id: "))
Name = input("Name: ")
Price = int(input("Price: "))
Data = [ID, Name, Price]
pickle.dump(Data,f)
ch = input("More? (y/n): ")
if ch in 'nN':
break
f.close()

def SHOWHIGH():
f = open("Plants.dat", "rb")
try:
while True:
data = pickle.load(f)
If data[2]>500:
print(data)
except EOFError:
f.close()

2. A binary file "Book.dat" has a structure [BookNo, BookName, Author,


Price]
a. Write a user defined function CreateFile() to input data for a
record and add to Book.dat
b. 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.

import pickle

def CreateFile():
f = open("Book.dat", "wb")
while True:
BookNo = int(input("Book No.: "))
BookName = input("Book Name: ")
Author = input("Author: ")
Price = int(input(“Price”: )
Data = [BookNo, BookName, Author, Price]
pickle.dump(Data,f)
ch = input("More? (y/n): ")
if ch in 'nN':
break
f.close()

def CountRec():
f = open(Book.dat", "rb")
Author = input("Author : ")
count = 0
try:
while True:
data = pickle.load(f)
if data[2] == Author:
count += 1
except EOFError:
f.close()
return count

3. Consider a binary file, Cinema.dat containing information in the


following structure : [Mno, Mname, Mtype] . Write a function,
search_copy(), that reads the content from the file Cinema.dat and
display all the details of the "Comedy" movie type.
import pickle

def CreateFile():
f = open("Cinema.dat", "wb")
while True:
MNo = int(input("Movie No.: "))
MName = input("Movie Name: ")
Mtype = input("Movie Type: ")
Data = [MNo, MName, Mtype]
pickle.dump(Data,f)
ch = input("More? (y/n): ")
if ch in 'nN':
break
f.close()

def searh_copy():
f = open(“Cinema.dat", "rb")
try:
while True:
data = pickle.load(f)
if data[2] == “Comedy”:
print(data)
except EOFError:
f.close()

4. Consider a binary file, Cinema.dat containing information in the


following structure: [Mno, Mname, Mtype] .Write a function,
search_copy(), that reads the content from the file Cinema.dat and
copies all the details of the "Comedy" movie type to file named
movie.dat.

import pickle

def CreateFile():
f = open("Cinema.dat", "wb")
while True:
MNo = int(input("Movie No.: "))
MName = input("Movie Name: ")
Mtype = input("Movie Type: ")
Data = [MNo, MName, Mtype]
pickle.dump(Data,f)
ch = input("More? (y/n): ")
if ch in 'nN':
break
f.close()

def searh_copy():
f = open(“Cinema.dat", "rb")
f1 = open(“Movie.dat”, “wb”)
try:
while True:
data = pickle.load(f)
if data[2] == “Comedy”:
pickle.dump(data, f1)
except EOFError:
f.close()

You might also like