Thursday, 16 July 2020

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.


PROGRAM:

1.
 

2.



3.


4.


OUTPUT:

1.



2.


3.


4.


5.


6.


7.




#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 pickle
import sys
dict={}
def write_in_file():
    file=open("D:\\stud2.dat","ab")  #a-append,b-binary
    no=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 file
    file.close()
 
def display():
    #read from file and display
    file=open("D:\\stud2.dat","rb")   #r-read,b-binary
    try:
        while True:
            stud=pickle.load(file)   #write to the file
            print(stud)
    except EOFError:
        pass
    file.close()
 
def search():
    file=open("D:\\stud2.dat","rb")   #r-read,b-binary
    r=int(input("enter the rollno to search: "))
    found=0
    try:
        while True:
            data=pickle.load(file)   #read from file
            if data["roll"]==r:
                print("The rollno =",r," record found")
                print(data)
                found=1
                break
    except EOFError:
        pass
    if found==0:
        print("The rollno =",r," record is not found")
    file.close()
 
#main program
#Prepared by Ramesha K S

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
>>>
 RESTART: C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\p-rks-f-prac-30.py
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: 1
enter the name: Abhijith
MENU
 1-Write in a file
 2-display
 3-search
 4-exit

Enter your choice = 2
{'roll': 1, 'name': 'Abhijith'}
MENU
 1-Write in a file
 2-display
 3-search
 4-exit

Enter your choice = 1
ENTER NO OF STUDENTS: 2
Enter details of student  1
Enter roll number: 2
enter the name: Mohith
Enter details of student  2
Enter roll number: 3
enter the name: Akshath
MENU
 1-Write in a file
 2-display
 3-search
 4-exit

Enter your choice = 2
{'roll': 1, 'name': 'Abhijith'}
{'roll': 2, 'name': 'Mohith'}
{'roll': 3, 'name': 'Akshath'}
MENU
 1-Write in a file
 2-display
 3-search
 4-exit

Enter your choice = 3
enter the rollno to search: 2
The rollno = 2  record found
{'roll': 2, 'name': 'Mohith'}
MENU
 1-Write in a file
 2-display
 3-search
 4-exit

Enter your choice = 3
enter the rollno to search: 6
The rollno = 6  record is not found
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: 5
enter the name: Bidya
MENU
 1-Write in a file
 2-display
 3-search
 4-exit

Enter your choice = 2
{'roll': 1, 'name': 'Abhijith'}
{'roll': 2, 'name': 'Mohith'}
{'roll': 3, 'name': 'Akshath'}
{'roll': 5, 'name': 'Bidya'}
MENU
 1-Write in a file
 2-display
 3-search
 4-exit

Enter your choice = 3
enter the rollno to search: 5
The rollno = 5  record found
{'roll': 5, 'name': 'Bidya'}
MENU
 1-Write in a file
 2-display
 3-search
 4-exit

Enter your choice = 4
 Thank you
>>>

Binary File:


1 comment: