0% found this document useful (0 votes)
5 views1 page

CSpracticalSet1

The document provides a Python script for managing student records using a binary file. It allows users to add student information and search for students by their roll number through a simple menu interface. The script utilizes the pickle module for serialization and deserialization of student data stored in a binary file.

Uploaded by

nik9721huma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

CSpracticalSet1

The document provides a Python script for managing student records using a binary file. It allows users to add student information and search for students by their roll number through a simple menu interface. The script utilizes the pickle module for serialization and deserialization of student data stored in a binary file.

Uploaded by

nik9721huma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

#First we import the pickle module to read and write the binary file in Python

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

You might also like