CLASS:-XII [COMPUTER SCI.
]
SESSION: 2022-23
TOPIC: BINARY FILE HANDLING IN PYTHON
[PRACTICE- ASSIGNMENTS-] BASED ON LIST DATA TYPE:
A binary file “[Link]” has structure
[admission_number, Name,Percentage].
Write a function countrec() in Python that would read contents of thefile “[Link]” and input()
information/upate() information/Delete() information display/search_info() according to rollno
and count_recored of the details of those students whose percentage isabove 75. Also display
number of students scoring above 75%
Solution:
import os
import pickle
#fUNCTION -1 TO write the information#
def write_data():
f1=open("[Link]","wb")
record=[]
while(True):
rno=int(input("Enter Rollno="))
name=input("ENter Name=")
marks=int(input("Enter Marks="))
data=[rno,name,marks]
[Link](data)
ch=input("Do you want to continue Press Y/N")
if(ch=='n' or ch=='N'):
break
[Link](record,f1)
[Link]()
#FUNCTION -2 Count Record whose per is more than 75%#
def count_rec() :
f1=open("[Link]","rb")
data= [Link](f1)
c1=0
for rec in data:
if(rec[2]>75): #marks/Per more than 75
c1=c1+1
print("Rollno=",rec[0], "Name=",rec[1],"Marks=",rec[2])
print("total Students whose per is more than 75%=", c1)
[Link]()
#FUNCTION -3 to Display all records with loop#
def display_all_record():
f1=open("[Link]","rb")
[Link](0)
data= [Link](f1)
print("Records of student=")
for r in data: #Display each record
print(r)
[Link]()
#FUNCTION -4 to Search the Given Record#
def search():
f1=open("[Link]","rb")
data= [Link](f1)
final=0
r1= int(input("Enter Roll No="))
for rec in data:
if(rec[0]==r1): #searching rollno from file
print("Record is found=")
print("Roll no=",rec[0])
print("Name=", rec[1])
print("Marks=",rec[2])
final=1
break
if(final==0):
print("Record is not found=")
[Link]()
#FUNCTION -5 to Delete the record from the file#
def delete1():
f1=open("[Link]","rb")
ftemp=open("[Link]",'wb')
rec=[] #empty list
final=0
rn= int(input("Enter Roll No for deletion=>"))
data= [Link](f1)
for r in data:
if(r[0]==rn):
print("Record Is Found=")
print(r)
print("Record is deleted=")
else:
d=[r[0],r[1],r[2]]
[Link](d)
[Link](rec,ftemp)
[Link]()
[Link]()
[Link]("[Link]") #remove the file
[Link]("[Link]", "[Link]")
#again change the name of file
#FUNCTION -6 to Update the Given Record#
def update():
f1=open("[Link]","rb+")
data= [Link](f1)
final=0
r1= int(input("Enter Roll No="))
for rec in data:
if(rec[0]==r1):
print("Record is found=")
print("Current Roll no=",rec[0])
print("Current Name=", rec[1])
print("Current Marks=",rec[2])
#Update/Modify the recored
rec[0]=int(input("Enter Rollno="))
rec[1]=input("ENter Name=")
rec[2]=int(input("Enter Marks="))
final=1
break
if(final==0):
print("Record is not found=")
else:
[Link](0)
[Link](data,f1) #updated recored
[Link]()
Just Complete the Assignments and write down in your fair note
book.
[Assignments - 1]
Write a code that Write the information itemcode,ItemName,amount
according to user choice and then reads from a file “[Link]”
which has following information [code,productname, amount]
display each record and create function to search() record and
1] last find out the the sum of the each product amount.
2] Find and display all product whose price is more than 500.
[Assignments - 2]
A binary file [Link], containing records of following list
format: [code, name, country and total runs]
(i)Write a python function that display all records where player
name starts from 'A'
(ii)Write a python function that accept country as an argument
and count and display the number of players of that country.
(iii)Write a python function that add one record at the end of
file.
(iv) Write a python function that display players information
whose total runs is more than 500.
[Assignments - 3]
Write a function in python to create write() method to write
information of train in the form of List and search() method to
display details, whose destination is “Cochin” from binary file
“[Link]”. Assuming the binary file is containing the following
elements in the list:
[Link] Number [Link] Starting Point 3. Bus Destination
[Assignments - 4]
Given a binary file [Link], created using dictionary object
having keys: (empcode, name, and salary)
(i)Write a python function that add one more record at the end of
file.
(ii)Write a python function that display all employee records
whose salary is more that 30000
Solution:
import pickle
def write_dict():
data={ } #empty dict...
f = open("[Link]","wb")
while(True):
id1=input("Enter ID:")
name=input("Enter Name:")
salary = int(input("Enter Salary"))
#data={"cid":id1 ,"Name":name, "City":city}
data["cid"]= id1
data["Name"]=name
data["Salary"] =salary
[Link](data,f)
ch=input("Do you want to continue Press Y or N=")
if(ch=='n' or ch=='N'):
break
[Link]()
#Display All #
def show():
f1 = open("[Link]","rb")
c1={ } #Empty Dictionary
try:
while True:
c1 = [Link](f1)
print(c1)
except EOFError:
[Link]()
#End of Function#
# display all employee records whose salary is more that 30000
def search_rec():
f = open("[Link]","rb")
cust={ }
count =found =0
print("Information of Emp=")
try:
while True:
cust = [Link](f)
if (cust['Salary']>=30000):
print(cust)
count+=1
found=1
except EOFError:
[Link]()
if(found==0):
print("Record not found...")
else:
print("Record found...")
print("total no. of Emp whose sal is more than 30000=", count)
//END OF THE ASSIGNMENTS//