0% found this document useful (0 votes)
3 views

binary file search

The document outlines steps and Python code snippets for searching data in binary files, including searching for specific words, employee numbers, and student records. It demonstrates how to read records, check for existence, and count occurrences based on specified criteria. Various examples illustrate searching for words in a text, employee details in a file, and student performance records.

Uploaded by

Ritika Pandey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

binary file search

The document outlines steps and Python code snippets for searching data in binary files, including searching for specific words, employee numbers, and student records. It demonstrates how to read records, check for existence, and count occurrences based on specified criteria. Various examples illustrate searching for words in a text, employee details in a file, and student performance records.

Uploaded by

Ritika Pandey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

SEARCHING Part 1

IN BINARY
FILES
STEPS TO SEARCH THE DATA IN FILE:
Open the file in read mode.
Read the file content record by record(i.e., object by
object).
In every read record , look for the desired search-key.
If found, process as desired.
If not found , read the next record and look for the
desired search-key.
If search-key is not found in any of the records, report the
no such value found in the file.
WAP to search word 'the' is existing in
binary the file "myfile.dat" or not.
Content in the file:
The Gomti is the biggest river.
import pickle
with open("myfile.dat","rb+") as f:
s=pickle.load(f) The Gomti is the biggest river.
f=0
for x in s.split(): [‘The’,’Gomti’,’is’,’the’,’biggest’,’river’]
if x=='the':
print(" Data Found")
f=1
break
if f==0:
print("Data not Found")
WAP to search the longest word
in an existing binary file
"myfile.dat".
Content in the file:
The Gomti is the biggest river.
import pickle
s=''
with open("myfile.dat","rb+") as f:
s=pickle.load(f)
g=s.split()
The Gomti is the biggest river.
x=''
for i in g:
if len(i)>len(x):
x=i
print(" The longest word:",x)
SEARCHING IN Part 2
BINARY FILES
WAF FIND() to search employee number (received
as parameter) exist in the file emp.dat or not.(If
record exist display it otherwise display message
“eno 3 not found”)
File contains the record in the following the
following format:
{Record1:[eno,name,age,salary]}
def FIND(eno):
bfr=open("emp.dat","rb") {Record1:[1,’ajay’,24,40000]}
sc="not found"
{Record2:[3,’vijay’,24,50000]}
x=1
try: {Record3:[2,’abhay’,25,8000]}
while True:
st='Record'+str(x)
d=pickle.load(bfr)
l=d[st]
if l[0]==eno:
print(st,':',d[st])
sc="found"
x=x+1
except EOFError:
if sc=="not found":
print("Record not found")
FIND(3)
bfr.close()
WAP to search, name inputted by user exists in the file
emp.dat or not. (If record exist display message
“record found” otherwise display message “not
found”)

File contains the record in the following dictionary


format:
{'eno':1,"name":"ajay","age":25,"sal":45000}
{'eno':2,"name":“abhay","age":24,"sal":43000}
{'eno':3,"name":“vijay","age":26,"sal":46000}
import pickle abhai except EOFError:
d={} if found==0:
found=0 print("Not found")
x=input("enter name to search") else:
fh=open("emp.dat","rb+") print(“Record found“)
try: fh.close()
while 1: {'eno':1,"name":"ajay","age":25,"sal":45000}
d=pickle.load(fh) {'eno':2,"name":"vijay","age":24,"sal":43000}
if d['name']==x: {'eno':3,"name":"abhay","age":26,"sal":4600}
found=1
SEARCHING IN Part 3
BINARY FILES
WAP to search and count number of students in
“stu.dat” between 60 to 80 percentage.(If no such
record found print message “No record found” and
if found print number of records found.
File contains the record in this format:
{‘rno’:1,’’name’:”ajay”,’per’:65}
{‘rno’:2,’’name’:”vijay”,’per’:85}
{‘rno’:3,’’name’:”ravi”,’per’:60}
{‘rno’:4,’’name’:”kavi”,’per’:81}
import pickle
d={}
c=0
found=“N”
fh=open("stu.dat","rb+")
try: {‘rno’:1,’’name’:”ajay”,’per’:65}
while True:
d=pickle.load(fh) {‘rno’:2,’’name’:”vijay”,’per’:85}
if d['per']>=60 and d['per']<=80:
{‘rno’:3,’’name’:”ravi”,’per’:60}
c=c+1
found=“F” {‘rno’:4,’’name’:”kavi”,’per’:81}
except EOFError:
if found==“N”:
print("No record found")
else:
print(c,"students secured between 60 to 80")
fh.close()
WAP to search records where student name starts with
‘a’ from “student.dat” .If record found display its
record number otherwise display message “Sorry!”.
File contains the record in this format:
{'eno':1,"name":"ajay","age":25,"sal":45000}
{'eno':2,"name":"vijay","age":24,"sal":43000}
{'eno':3,"name":"abhay","age":26,"sal":46000}
import pickle
d={}
{‘rno’:1,’’name’:”ajay”,’per’:65}
found,P=0
fh=open("student.dat","rb+") {‘rno’:2,’’name’:”vijay”,’per’:85}
try: {‘rno’:3,’’name’:”abhay”,’per’:60}
while True:
d=pickle.load(fh)
P=P+1
if d['name'][0]==‘a':
print(“Record Number ”,P,” is having name starting with’a’)
found=1
except EOFError:
if found==0:
print(“Sorry!")
fh.close()

You might also like