You are on page 1of 6

CLASS:-XII [COMPUTER SCI.

]
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()

#FUNCTION -3 to Display all records with loop#


def display_all_record():
f1=open("file1.dat","rb")
f1.seek(0)
data= pickle.load(f1)
print("Records of student=")
for r in data: #Display each record
print(r)
f1.close()

#FUNCTION -4 to Search the Given Record#


def search():
f1=open("file1.dat","rb")
data= pickle.load(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=")
f1.close()
#FUNCTION -5 to Delete the record from the file#
def delete1():
f1=open("file1.dat","rb")
ftemp=open("ftemp.dat",'wb')
rec=[] #empty list
final=0
rn= int(input("Enter Roll No for deletion=>"))
data= pickle.load(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]]
rec.append(d)
pickle.dump(rec,ftemp)
f1.close()
ftemp.close()
os.remove("file1.dat") #remove the file
os.rename("ftemp.dat", "file1.dat")
#again change the name of file
#FUNCTION -6 to Update the Given Record#
def update():
f1=open("file1.dat","rb+")
data= pickle.load(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:
f1.seek(0)
pickle.dump(data,f1) #updated recored
f1.close()
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 “sales.dat”
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 players.dat, 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
“Bus.Dat”. Assuming the binary file is containing the following
elements in the list:

1.Bus Number 2.Bus Starting Point 3. Bus Destination


[Assignments - 4]

Given a binary file employee.dat, 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("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)

//END OF THE ASSIGNMENTS//

You might also like