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

Program 13

Uploaded by

rajputaanya613
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Program 13

Uploaded by

rajputaanya613
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAM 13:

Ques17: A binary file book.dat has a structure [book number,


book name, Author, price]
i) write a user defined function create file() to input data for
a record then add to Book.dat
ii) Write a function CountRec(Author) which accept the
author name as parameter and count and return the
number of book by the given author and store it in binary
file book.dat

import pickle
def createfile():
f=open(“Book.dat”, “wb”)
while True:
num=int(input (“Enter book numbers:-”))
name=input (“Enter book name:-”)
aut=input (“Enter author:-”)
pri=float (input (“Enter price of book:-”))
list=[num,name,aut,pri]
pickle.dump(lst,f)
choice=input (“for exit (Enter exit):-”)
Print ()
if choice==“exit” or choice==“Exit”:
print (“Thank you ”)
print ()
break
f.close()
def count Rec():
f=open(“Book.dat” , “rb”)
count=0

try:
while True:
data=pickle.load(f)
if data [2]==aut:
count+=1
except EOFError:
f.close()
Print (“Number of book with author name” , aut,“=”,count)

Create file()
Print (“for searching-”)
aut =input (“Enter Author:-”)
countRec(aut)
OUTPUT:
PROGRAM 10:
Ques10: write a function in python to create the text
file

###create new TXT file


##f=open(“C:/Temp/testing.txt”, “W”)
##

###create New DAT file


##f=open (“C:/Temp/testing.dat”, “W”)

#Write to Existing TXT file


F=open(“C:/Temp/testing.txt” , “W”)
F.write(“Adding some random dat”)
F.close()
OUTPUT:

You might also like