Professional Documents
Culture Documents
Rishabh Set - 22222
Rishabh Set - 22222
Date: 19-01-2024
Class: 12
Section: A
SET – 02
Practical - 1:
Create a binary file with roll number, name and marks of some students. Input a roll number
#First we import the pickle module to read and write the binary file in Python
import pickle
S={}
while True:
1 SET - 2
print("Press 1: For Add Student")
if choice == 1:
#Use open() for create and open the binary file in append mode
f=open('stud.dat','ab')
S['RollNo']=rno
S['Name']=name
S['Marks']=marks
#dump() of pickle module used to write the content in the binary file
pickle.dump(S,f)
f.close()
2 SET - 2
f=open('stud.dat','rb+')
f.seek(0,0)
m=0
try:
while True:
pos=f.tell()
S=pickle.load(f)
if S["RollNo"] == rno:
f.seek(pos)
S["Marks"]=marks
pickle.dump(S,f)
m=m+1
except EOFError:
f.close()
if m==0:
else:
3 SET - 2
f=open('stud.dat','rb')
try:
while True:
S=pickle.load(f)
print(S)
except EOFError:
f.close()
else:
break
Output:
4 SET - 2
Press 2: For Update Student
5 SET - 2
{'RollNo': 2, 'Name': 'Aman', 'Marks': 18}
Practical - 2:
(a) Display the details of all employees in descending order of their salary.
(c) Display the city with number of employees belongs to that city.
6 SET - 2