0% found this document useful (0 votes)
1K views

BINARY FILE Question Bank Solutions

The document contains 6 questions about binary files and Python functions. It discusses reading from and writing to binary files using pickle, and creating functions to count records, search for matching values, and shift contacts between blocked and unblocked files.

Uploaded by

saravanan
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)
1K views

BINARY FILE Question Bank Solutions

The document contains 6 questions about binary files and Python functions. It discusses reading from and writing to binary files using pickle, and creating functions to count records, search for matching values, and shift contacts between blocked and unblocked files.

Uploaded by

saravanan
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/ 10

BINARY FILE

1. 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 75. Also display number

of students scoring above 75%

Ans:

import pickle

def countrecw():

f = open('STUDENT.DAT','ab')

rec = [['Admission_number', 'Name', 'Percentage']]

ans ='y'

while ans.lower() == 'y':

a_no=int(input('Enter Admission number: '))

name=input('Enter Name: ')

percent=int(input('Enter Percentage: '))

r = rec.append([a_no, name, percent])

ans = input('continue ? (y/n): ')

#print(r)

pickle.dump(rec,f)

f.close()
countrecw()

def countrec():

f = open('STUDENT.DAT','rb')

records = []

c =0

while True:

try:

records = pickle.load(f)

#print(records)

for line in records:

if line[2] == 'Percentage':

pass

elif line[2] >= 75:

c+=1

print(line)

except:

print('Number of students scoring above 75% : ' , c)

break

countrec()

2. A binary file "Book.dat" has structure:

[BookNo, Book_Name, Author,Price].

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

Ans:

import pickle

def CreateFile():

f = open('Book.dat','wb')

rec = [['BookNo', 'Book_Name', 'Author', 'Price']]

ans ='y'

while ans.lower() == 'y':

b_no=int(input('Enter Book number: '))

b_name=input('Enter Book Name: ')

author_name=input('Enter Author Name: ')

price=int(input('Enter Price: '))

rec.append([b_no, b_name, author_name, price])

ans = input('continue ? (y/n): ')

#print(rec)

pickle.dump(rec,f)

f.close()

CreateFile()

def CountRec(Author):

f = open('Book.dat','rb')

records = []
c =0

while True:

try:

records = pickle.load(f)

#print(records)

for line in records:

if line[2] == 'Author':

pass

elif line[2] == Author:

c+=1

#print(line)

except:

print('Number of books by the given Author : ' , c)

break

Author=input('Enter Author Name to search: ')

CountRec(Author)

3. A binary file "Toy.dat" has structure [TID, Toy, Status, MRP].

i. Write a user defined function CreateFile() to input data for a record and add to
"Toy.dat"

ii. Write a function OnOffer() in Python to display the detail of those Toys, which
has status as "ON OFFER" from "Toy.dat" file.

Ans:

import pickle
def CreateFile():

f = open('Toy.dat','wb')

rec = [['TID','Toy','Status','MRP']]

ans ='y'

while ans.lower() == 'y':

TID=int(input('Enter Toy ID: '))

Toy=input('Enter Toy Name: ')

Status=input('Enter Status: ')

MRP=int(input('Enter MRP: '))

rec.append([TID,Toy,Status,MRP])

ans = input('continue ? (y/n): ')

#print(rec)

pickle.dump(rec,f)

f.close()

CreateFile()

def OnOffer():

f = open('Toy.dat','rb')

records = []

c =0

while True:

try:

records = pickle.load(f)

#print(records)
for line in records:

if line[2] == "ON OFFER":

print(line)

except:

break

OnOffer()

4. Considering the following definition of dictionary MULTIPLEX, write a method


in python to search and display all the content in a pickled file
CINEMA.DAT,where MTYPE key of the dictionary is matching with the value
"Comedy".

MULTIPLEX = {'MNO': _____, 'MNAME": _____, 'MTYPE': _____}

Ans:

import pickle

def CreateFile():

f = open ('CINEMA.DAT','wb')

rec = []

ans = 'y'

while ans.lower()=='y':

MNO = int(input('Enter MNO: '))

MNAME = input('Enter MNAME: ')

MTYPE = input('Enter MTYPE: ')

MULTIPLEX = {'MNO':MNO,'MNAME':MNAME,'MTYPE':MTYPE}

rec.append(MULTIPLEX)

ans = input("continue?(y/n):")
#print(rec)

pickle.dump(rec,f)

f.close()

CreateFile()

def readFile():

f = open ('CINEMA.DAT','rb')

nested_records = []

while True:

try:

nested_records = pickle.load(f)

#print(nested_records)

for d in nested_records:

if d['MTYPE'] == "Comedy":

print (d)

except :

break

readFile()

5. As a Python expert, help to Vaishnavi to complete the following code based on


the requirement given above:

import ______________ #Statement 1

def shift_contact( ):

fin = open(“phonebook.dat”,‟rb‟)

fblock = open( ) #Statement 2


funblock = open( ___________________ ) #Statement 3

while True :

try:

rec = __________________ # Statement 4

if rec*“blocked”+ == „ Y‟:

pickle___________________ #Statement 5

if rec*“blocked”+ == „ N‟:

pickle__________________ # Statement 6

except:

break

shift_contact()

(i) Which module should be imported in the program? (Statement 1)

(ii) Write the correct statement required to open a blocked.dat and


unblocked.dat binary files (Statement 2 and 3)

(iii) which statement should Vaishnavi use in statement 4 to read the data from
thebinary file, phonebook.dat

(iv) which statement should Vaishnavi use in statement 5 and 6 to write data to
theblocked.dat and unblocked.dat

Ans:

(i) import pickle # Statement 1

(ii) fblock = open("blocked.dat",'rb') # Statement 2

funblock = open("unblocked.dat",'rb') # Statement 3

(iii) rec = pickle.load(fin) # Statement 4


(iv) pickle.dump(rec,fblock) #Statement 5

pickle.dump(rec,funblock) # Statement 6

6. 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 75. Also display number of students scoring above 75%.

Ans:

import pickle

def countrecw():

f = open('STUDENT.DAT','ab')

rec = [['Admission_number', 'Name', 'Percentage']]

ans ='y'

while ans.lower() == 'y':

a_no=int(input('Enter Admission number: '))

name=input('Enter Name: ')

percent=int(input('Enter Percentage: '))

r = rec.append([a_no, name, percent])

ans = input('continue ? (y/n): ')

#print(r)

pickle.dump(rec,f)

f.close()

countrecw()

def countrec():

f = open('STUDENT.DAT','rb')
records = []

c =0

while True:

try:

records = pickle.load(f)

#print(records)

for line in records:

if line[2] == 'Percentage':

pass

elif line[2] >= 75:

c+=1

print(line)

except:

print('Number of students scoring above 75% : ' , c)

break

countrec()

You might also like