CSpracticalSet1
CSpracticalSet1
import pickle
#Initialize an Empty dictionary
S={}
while True:
#Create a menu for different type of options
print("Press 1 : For ADD Student")
print("Press 2 : For Search Student")
print("Press 3 : For Exit")
#Input Choice from the user
choice = int(input("Enter Your Choice:"))
if choice == 1:
#Use open() for create and open the binary file in append mode
F = open('stud.dat','ab')
rno = int(input("Enter the RollNo of Student:"))
name = input("Enter the Name of the Student:")
S['RollNo']= rno
S['Name']= name
#dump() of pickle module used to write the content in the binary file
pickle.dump(S,F)
#close the file using close()
F.close()
elif choice == 2:
#Now open the binary file in read mode
F = open('stud.dat','rb')
rno = int(input("Enter RollNo of the Student to be search:"))
K={}
m=0
try:
while True:
#load() of pickle module use to read the binary file
K = pickle.load(F)
if K["RollNo"] == rno:
print(K)
m = m+1
except EOFError:
F.close()
if m==0:
print("Student NOT Found")
else:
break