BINARY FILE Question Bank Solutions
BINARY FILE Question Bank Solutions
Ans:
import pickle
def countrecw():
f = open('STUDENT.DAT','ab')
ans ='y'
#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)
if line[2] == 'Percentage':
pass
c+=1
print(line)
except:
break
countrec()
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')
ans ='y'
#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)
if line[2] == 'Author':
pass
c+=1
#print(line)
except:
break
CountRec(Author)
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'
rec.append([TID,Toy,Status,MRP])
#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:
print(line)
except:
break
OnOffer()
Ans:
import pickle
def CreateFile():
f = open ('CINEMA.DAT','wb')
rec = []
ans = 'y'
while ans.lower()=='y':
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()
def shift_contact( ):
fin = open(“phonebook.dat”,‟rb‟)
while True :
try:
if rec*“blocked”+ == „ Y‟:
pickle___________________ #Statement 5
if rec*“blocked”+ == „ N‟:
pickle__________________ # Statement 6
except:
break
shift_contact()
(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:
pickle.dump(rec,funblock) # Statement 6
Ans:
import pickle
def countrecw():
f = open('STUDENT.DAT','ab')
ans ='y'
#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)
if line[2] == 'Percentage':
pass
c+=1
print(line)
except:
break
countrec()