Vaibhav Sharma 12-A Roll No. 34assignment - 3 Binary File

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

BINARY FILE ASSIGNMENT

S. No. QUESTIONS
Following is the structure of each word in a data file named
“PRODUCT.DAT”:
{“prod_code”:value, ”prod_desc”:value, ”stock”:value}
The values for prod_code and prod_desc are strings and the value for
1. stock is an integer.
Write a function in PYTHON to update the file with a new value of
stock. The stock and the product code, whose stock is to be updated,
are to be input during the execution of the function.
Given a binary file “STUDENT.DAT”, containing records of the
following type:
[S_Admno, S_Name, Percentage]
Where these three values are:
S_Admno – Admission Number of Student (String)
2. S_Name – Name of Student (String)
Percentage – Marks Percentage of Student (Float)
Write a function in PYTHON that would read contents of the file
“STUDENT.DAT” and display the details of those students whose
percentage is above 75.
Assuming the tuple Vehicle as follows:
(vehicletype, no_of_wheels)
Where vehicletype is a string and no_of_wheels is an integer.
3. Write a function showfile() to read all the records present in an
already existing binary file SPEED.DAT and display them on the
screen, also count the number of records present in the file.
Write a function in PYTHON to search for a BookNo from a binary
file “BOOK.DAT”, assuming the binary file is containing the records
4. of the following type:
{"BookNo":value, "Book_name":value}
Assume that BookNo is an integer.
Assuming that a binary file VINTAGE.DAT contains records of the
following type, write a function in PYTHON to read the data
5. VINTAGE.DAT and display those vintage vehicles, which are priced
between 200000 and 250000.
[VNO, VDesc, price]
Write a function in PYTHON to search for a laptop from a binary file
“LAPTOP.DAT” containing the records of following type. The user
should enter the model number and the function should display the
6. details of the laptop.
[ModelNo, RAM, HDD, Details]
Where ModelNo, RAM, HDD are integers, and Details is a string.
Write a function in PYTHON to search for the details (Number and
Calls) of those mobile phones which have more than 1000 calls from a
7. binary file “mobile.dat”. Assuming that this binary file contains
records of the following type:
(Number,calls)
Write a function in PYTHON to read the records from binary file
GAMES.DAT and display the details of those games, which are meant
8. for children of AgeRange “8 to 13”. Assume that the file GAMES.DAT
contains records of the following type:
[GameCode, GameName, AgeRange]
Write a function in PYTHON to read each record of a binary file
ITEMS.DAT, find and display those items which costs less than 2500.
9. Assume that the file ITEMS.DAT is created with the help of objects
of the following type:
{"ID":string, "GIFT":string, "Cost":integer}
Write a definition for function BUMPER() in PYTHON to read each
object of a binary file GIFTS.DAT, find and display details of those
10. gifts, which have remarks as “ON DISCOUNT”. Assume that the file
GIFTS.DAT is created with the help of lists of following type:
(ID, Gift, Remarks, Price)
1. Following is the structure of each word in a data file named “PRODUCT.DAT”:
{“prod_code”:value, ”prod_desc”:value, ”stock”:value}
The values for prod_code and prod_desc are strings and the value for stock is an integer.
Write a function in PYTHON to update the file with a new value of stock. The stock and
the product code, whose stock is to be updated, are to be input during the execution of
the function.
Ans. import pickle
def bf_update():
f = open("Product.dat","rb+")
L = pickle.load(f)
r = input("Enter Product Code: ")
found = False
for x in L:
prod_code = x[0]
if prod_code==r:
x[2]=input("Enter Stock to Update: ")
found = True
break
if not found:
print("Invalid Product Code!")
else:
f.seek(0)
pickle.dump(L,f)
print("Record Updated.....")
f.close()
bf_update()
2. Given a binary file “STUDENT.DAT”, containing records of the following type:
[S_Admno, S_Name, Percentage]
Where these three values are:
S_Admno – Admission Number of Student (String)
S_Name – Name of Student (String)
Percentage – Marks Percentage of Student (Float)
Write a function in PYTHON that would read contents of the file “STUDENT.DAT” and
display the details of those students whose percentage is above 75.
Ans. import pickle
def bf_search():
f=open("Student.dat","rb")
print("*"*78)
print("Data stored in File....")
try:
num=0
while True:
rec=pickle.load(f)
for i in rec:
if i[1]>=str(75):
print("Admission No.: ",i[0])
print("Name: ",i[1])
print("."*78)
num+=1
except EOFError:
f.close()
print("Total No. of Records: ",num)
bf_search()
3. Assuming the tuple Vehicle as follows:
(vehicletype, no_of_wheels)
Where vehicletype is a string and no_of_wheels is an integer.
Write a function showfile() to read all the records present in an already existing binary
file SPEED.DAT and display them on the screen, also count the number of records
present in the file.
Ans. import pickle
def read():
f = open("Speed.dat","rb")
print("*"*78)
print("Data stored in File....")
try:
c=0
while True:
data= pickle.load(f)
for record in data:
print("Vehicle Type: ",record[0])
print("No. Of Wheels:",record[1])
print("."*78)
c+=1
except EOFError:
f.close()
print("Total No. Of Record: ",c)
read()
4. Write a function in PYTHON to search for a BookNo from a binary file “BOOK.DAT”,
assuming the binary file is containing the records of the following type:
{"BookNo":value, "Book_name":value}
Assume that BookNo is an integer.
Ans. import pickle
def bf_search():
import pickle
f=open("Book.dat","rb")
L=pickle.load(f)
book_no=int(input('Enter Book no. to find: '))
currentline = 1
for line in L:
if(currentline == book_no):
print(line)
break
currentline = currentline +1
bf_search()
5. Assuming that a binary file VINTAGE.DAT contains records of the following type, write a
function in PYTHON to read the data VINTAGE.DAT and display those vintage vehicles,
which are priced between 200000 and 250000.
[VNO, VDesc, price]
Ans. import pickle
def display():
f=open("Vintage.dat","rb")
print("*"*78)
print("Data stored in File....")
try:
num=0
while True:
rec=pickle.load(f)
for i in rec:
if i[1]>=str(200000):
print("Vintage Vehicle No.: ",i[0])
print("Vintage Vehicle Description.: ",i[1])
print("."*78)
num+=1
except EOFError:
f.close()
print("Total No. of Records: ",num)
display()
6. Write a function in PYTHON to search for a laptop from a binary file “LAPTOP.DAT”
containing the records of following type. The user should enter the model number and
the function should display the details of the laptop.
[ModelNo, RAM, HDD, Details]
Where ModelNo, RAM, HDD are integers, and Details is a string.
Ans. import pickle
def search():
file = open("Laptop.dat","rb")
model_find=input('Model no. of the Laptop to find: ')
try:
while True:
record = pickle.load(file)
for i in record:
if i[0] == model_find:
print("Model No.: ",i[0])
print("RAM Size: ",i[1])
print("HDD: ",i[2])
print("Details: ",i[3])
print("."*78)
except EOFError:
file.close()
search()
7. Write a function in PYTHON to search for the details (Number and Calls) of those mobile
phones which have more than 1000 calls from a binary file “mobile.dat”. Assuming that
this binary file contains records of the following type:
(Number,calls)
Ans. import pickle
def bf_search():
f=open("Mobile.dat","rb")
print("*"*78)
print("Data stored in File....")
try:
num=0
while True:
rec=pickle.load(f)
for i in rec:
if i[1]>=str(1000):
print("Number: ",i[0])
print("No. of Calls: ",i[1])
print("."*78)
num+=1
except EOFError:
f.close()
print("Total No. of Records: ",num)
bf_search()
8. Write a function in PYTHON to read the records from binary file GAMES.DAT and display
the details of those games, which are meant for children of AgeRange “8 to 13”. Assume
that the file GAMES.DAT contains records of the following type:
[GameCode, GameName, AgeRange]
Ans. import pickle
def bf_read():
f = open("Games.dat","rb")
print("*"*78)
print("Data stored in File....")
try:
c=0
while True:
data=pickle.load(f)
for i in data:
if i[2]>=str(8-13):
print("Game Code: ",i[0])
print("Game Name:",i[1])
print("."*78)
c+=1
except EOFError:
f.close()
print("Total No. Of Record: ",c)
bf_read()
9. Write a function in PYTHON to read each record of a binary file ITEMS.DAT, find and
display those items which costs less than 2500. Assume that the file ITEMS.DAT is
created with the help of objects of the following type:
{"ID":string, "GIFT":string, "Cost":integer}
Ans. import pickle
def bf_search():
f=open("Items.dat","rb")
print("*"*78)
print("Data stored in File....")
try:
num=0
while True:
rec=pickle.load(f)
for i in rec:
if i[2]>=str(2500):
print("ID: ",i[0])
print("Gift: ",i[1])
print("."*78)
num+=1
except EOFError:
f.close()
print("Total No. of Records: ",num)
bf_search()
10. Write a definition for function BUMPER() in PYTHON to read each object of a binary file
GIFTS.DAT, find and display details of those gifts, which have remarks as “ON
DISCOUNT”. Assume that the file GIFTS.DAT is created with the help of lists of following
type:
(ID, Gift, Remarks, Price)
Ans. import pickle
def bf_ search():
f=open("Gifts.dat","rb")
print("*"*78)
print("Data stored in File....")
try:
num=0
while True:
rec=pickle.load(f)
for i in rec:
if i[2]>=str(2500):
print("ID: ",i[0])
print("Gift: ",i[1])
print("Remarks: ",i[2])
print("."*78)
num+=1
except EOFError:
f.close()
print("Total No. of Records: ",num)
bf_search()

You might also like