



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Create a binary file with roll number, name and marks of some students. Input a roll number and update the marks of specific student.
#First we import the pickle module to read and write the binary file in Python
import pickle
#Initialize a Empty dictionary
S={}
while True:
#Create a menu for different type of options
print("Press 1: For Add Student")
print("Press 2: For Update 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 roll no. of the student : ")) name=input("Enter the name of the student: ") marks=int(input("Enter the marks of the student: ")) 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) #close the file using close() f.close()
elif choice ==2:
#Now open the binary file in read mode
f=open('stud.dat','rb') try: while True: S=pickle.load(f) #Display the details of the student print(S) except EOFError: f.close() else: break
Press 1: For Add Student
Press 2: For Update Student
Press 3: For Exit
Enter Your Choice: 1
Enter the roll no. of the student : 1
Enter the name of the student: Amit
Enter the marks of the student: 22
Press 1: For Add Student
Press 2: For Update Student
Press 3: For Exit
Enter Your Choice: 1
Enter the roll no. of the student : 2
Enter the name of the student: Aman
Enter the marks of the student: 18
Press 1: For Add Student
Press 2: For Update Student
Press 3: For Exit
Enter Your Choice: 1
Enter the roll no. of the student : 3
Enter the name of the student: Ankit
Enter the marks of the student: 28
Press 1: For Add Student
Press 2: For Update Student
Press 3: For Exit
Enter Your Choice: 2
Enter the roll no. of the student to be updated: 3
Enter the updated marks of the student: 25
{'RollNo': 1, 'Name': 'Amit', 'Marks': 22}