Create a binary file with name and roll number. Search for a given roll number and display the name, if not found display appropriate message.
Here I have mentioned the Sample Output & Source code of a binary file with name and roll number. Search for a given roll number and display the name, if not found display appropriate message.
Program:
Aim:
To Create a binary file with name and roll number. Search for a given roll number and display the name, if not found display appropriate message.
Procedure:
Step1: Here is the first process of the program to import the module for Binary file which we call its as "pickle" & sys.
Step 2: Here is the second process of the program to define display() function.
Step 3: Here is the third process of the program to define search() for search data from binary file.
Step 4: Here is the fourth process of the program to define main program.
Sample Output:
Source Code:
#Practical Program Create a binary file with name and roll number.#Search for a given roll number and display the name, if not found#display appropriate message.import pickleimport sysdict={}def write_in_file():file=open("D:\\stud2.dat","ab") #a-append,b-binaryno=int(input("ENTER NO OF STUDENTS: "))for i in range(no):print("Enter details of student ", i+1)dict["roll"]=int(input("Enter roll number: "))dict["name"]=input("enter the name: ")pickle.dump(dict,file) #dump-to write in student filefile.close()def display():#read from file and displayfile=open("D:\\stud2.dat","rb") #r-read,b-binarytry:while True:stud=pickle.load(file) #write to the fileprint(stud)except EOFError:passfile.close()def search():file=open("D:\\stud2.dat","rb") #r-read,b-binaryr=int(input("enter the rollno to search: "))found=0try:while True:data=pickle.load(file) #read from fileif data["roll"]==r:print("The rollno =",r," record found")print(data)found=1breakexcept EOFError:passif found==0:print("The rollno =",r," record is not found")file.close() #main program while True: print("MENU \n 1-Write in a file \n 2-display ") print(" 3-search\n 4-exit \n") ch=int(input("Enter your choice = ")) if ch==1: write_in_file() if ch==2: display() if ch==3: search() if ch==4: print(" Thank you ") sys.exit()
Output
MENU
1-Write in a file 2-display 3-search 4-exit Enter your choice = 1 ENTER NO OF STUDENTS: 1 Enter details of student 1 Enter roll number: 01 enter the name: Ravi MENU 1-Write in a file 2-display 3-search 4-exit