11 Binary File Handling Assignment
11 Binary File Handling Assignment
11 Binary File Handling Assignment
]
SESSION: 2022-23
TOPIC: BINARY FILE HANDLING IN PYTHON
[PRACTICE- ASSIGNMENTS-] BASED ON LIST DATA TYPE:
A binary file “FILE1.DAT” has structure
[admission_number, Name,Percentage].
Write a function countrec() in Python that would read contents of thefile “file1.dat” 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("file1.dat","wb")
record=[]
while(True):
rno=int(input("Enter Rollno="))
name=input("ENter Name=")
marks=int(input("Enter Marks="))
data=[rno,name,marks]
record.append(data)
ch=input("Do you want to continue Press Y/N")
if(ch=='n' or ch=='N'):
break
pickle.dump(record,f1)
f1.close()
#FUNCTION -2 Count Record whose per is more than 75%#
def count_rec() :
f1=open("file1.dat","rb")
data= pickle.load(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)
f1.close()
[Assignments - 1]
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]
[Assignments - 3]
(i)Write a python function that add one more record at the end of
file.
Solution:
import pickle
def write_dict():
data={ } #empty dict...
f = open("cust1.dat","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
pickle.dump(data,f)
ch=input("Do you want to continue Press Y or N=")
if(ch=='n' or ch=='N'):
break
f.close()
#Display All #
def show():
f1 = open("cust1.dat","rb")
c1={ } #Empty Dictionary
try:
while True:
c1 = pickle.load(f1)
print(c1)
except EOFError:
f1.close()
#End of Function#
# display all employee records whose salary is more that 30000
def search_rec():
f = open("cust1.dat","rb")
cust={ }
count =found =0
print("Information of Emp=")
try:
while True:
cust = pickle.load(f)
if (cust['Salary']>=30000):
print(cust)
count+=1
found=1
except EOFError:
f.close()
if(found==0):
print("Record not found...")
else:
print("Record found...")
print("total no. of Emp whose sal is more than 30000=", count)