0% found this document useful (0 votes)
107 views2 pages

4.binary Create and Search

This Python program allows a user to create a binary file containing student roll numbers and names. It uses pickle to dump the records into the file. The user can then search the file by entering a roll number. If a matching record is found, the associated name is displayed. If no match is found, an appropriate message is shown.

Uploaded by

toxiccreapers
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)
107 views2 pages

4.binary Create and Search

This Python program allows a user to create a binary file containing student roll numbers and names. It uses pickle to dump the records into the file. The user can then search the file by entering a roll number. If a matching record is found, the associated name is displayed. If no match is found, an appropriate message is shown.

Uploaded by

toxiccreapers
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/ 2

4. To write a Python Program to Create a binary file with roll number and name.

Search for a given roll number and display the name, if not found display
appropriate message.

#Bin file create and search

import pickle
def create():
f=open(r'G:\Juliet 2023-2024\students.dat','wb')
rec=[]
while True:
rno=int(input("Enter roll no : "))
name=input('Enter name : ')
data=[rno,name]
rec.append(data)
ch=input('Do you want to continue (y/n) : ')
if ch in 'Nn':
break
pickle.dump(rec,f)
print('Record Added')
f.close()

def search():
f=open(r'G:\Juliet 2023-2024\students.dat','rb')
r=int(input('Enter Roll no. of student to search : '))
found=0
try:
while True:
d=pickle.load(f)
for i in d:
if i[0]==r:
print('The searched roll no is found : ', i)
found=1
break
except EOFError:
f.close()
if found==0:
print('The searched roll no is not found')

create()
search()

You might also like