22.06.2024 Binary File Complete
22.06.2024 Binary File Complete
# Write a user defined function CreateFile() to input data for a record and add to
# Book.dat .
import pickle
# def CreateFile():
# f=open("Book.Dat","wb")
# while True:
# bno=int(input("Enter Book Number : "))
# bname=input("Enter Book Name ")
# aname=input("Enter Author Name ")
# price=float(input("Enter Price "))
# b=[bno,bname,aname,price]
# pickle.dump(b,f)
# ch=input("Do you want to add more record Y/N ")
# if ch not in 'Yy':
# break
# f.close()
# def ReadFile():
# f=open("book.dat","rb")
# count=0
# while True:
# try:
# b = pickle.load(f)
# if b[3]>300:
# print("Book Number : ", b[0])
# print("Book Name : ", b[1])
# print("Author Name : ", b[2])
# print("Price : ", b[3])
# count = count + 1
#
# except:
# break
# print("Total No Of Records = ",count)
# f.close()
# CreateFile()
# ReadFile()
#
# '''Q7. A binary file “Toys.DAT” has structure (Toy_code, Toy_Name, Age_range).
# Write a function in Python that would read contents of the file “Toys.DAT” and
# display the details of those Toys, which are meant for children of AgeRange "5 to
8".'''
# import pickle
# def CreateFile():
# f=open("Toys.dat","wb")
# while True:
# code=input("Enter Toy Code ")
# name=input("Enter Toy Name ")
# agerange=input("Enter Age Range ")
# t=[code,name,agerange]
# pickle.dump(t,f)
# ch=input("Do you want to add more Record Y/N ")
# if ch not in 'Yy':
# break
# f.close()
# # CreateFile()
# def ReadFile():
# f=open("Toys.dat","rb")
# while True:
# try:
# t=pickle.load(f)
# if t[2]=="5 TO 8":
# print("Toy Code ",t[0])
# print("Toy Name ",t[1])
# print("Age Range ",t[2])
# except:
# break
# f.close()
# ReadFile()
#
#
# '''Q11. A binary file “School.DAT” has structure (Scode, Sname,No_Of_Teachers).
# Write a function in Python that would read contents of the file “School.DAT”,
find
# and display the total number of teachers, whose data is stored in the file and
display
# the same.'''
# import pickle
# def CreateFile():
# f=open("School.dat","wb")
# while True:
# scode=input("Enter School Code ")
# sname=input("Enter School Name ")
# noofteachers=int(input("Enter No Of Teachers "))
# s=[scode,sname,noofteachers]
# pickle.dump(s,f)
# ch=input("Do You Want to add More Record Y/N ")
# if ch not in 'Yy':
# break
# f.close()
# # CreateFile()
# def ReadFile():
# f=open("School.dat","rb")
# total=0
# while True:
# try:
# s=pickle.load(f)
# total=total+s[2]
# print("School Code ",s[0])
# print("School Name ",s[1])
# print("No of Teachers ",s[2])
# except:
# break
# f.close()
# print("Total No of Teachers = ",total)
# ReadFile()