Computer Science, asked by Tinab, 6 months ago

A binary file BOOK.DAT has structure (book no., book name ,author name and price ).
(i). Write a user defined function Create_File( ) to input data for a record and add to BOOK.DAT.
(ii). Write a user defined function Count_Rec( ) which accept the author name as parameter,
count and return number of a records given by the author in binary file BOOK.DAT.

Answers

Answered by duragpalsingh
0

Question:

A binary file BOOK.DAT has structure (book no., book name ,author name and price ).

(i). Write a user defined function Create_File( ) to input data for a record and add to BOOK.DAT.

import pickle

def createFile():

fobj=open("Book.dat","ab")

BookNo=int(input("Book Number : "))

Book_name=input("Name :")

Author = input(“Author: “)

Price = int(input("Price : "))

rec=[BookNo,Book_Name,Author,Price]

pickle.dump(rec,fobj)

fobj.close()

(ii). Write a user defined function Count_Rec( ) which accept the author name as parameter,count and return number of a records given by the author in binary file BOOK.DAT.

def CountRec(Author):

fobj=open("Book.dat","rb")

num = 0

try:

while True:

rec=pickle.load(fobj)

if Author==rec[2]:

num = num + 1

except:

fobj.close()

return num

Similar questions