binary file search
binary file search
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”)