Hii,
Can anyone provide me the python code for finding a number that is stored in a file .
The file contains 1 lakh numbers!!
And i want a python code that searches the file and find the desired element!!!!
Both linear search and binary search!!
i'm a python beginner!!!!
Plzz help urgent

100000 isn't that big. Why not just:

f=open(<your text file>)
for line in f:
  if <your search string> in line:
     <do your thing>
f.close()

or even:

f=open(<your text file>)
strA=f.read()
f.close()
if <your search string> in strA:
   <do your thing>

I always learn new things. 1 lakh = 10**5

This is the recommended approach to open a file and handle it since python 2.7:

with open("filename") as f:
    <handle content of the file>

Otherwise rrashkin's solution is good for linear search.

You say binary search in contrast to linear search.
I suppose you mean this by binary search.

Are you?

1 lakh of lines of file is not big enough to justify binary search. Use linear.

Binary search is only meaningfull if the file is ordered on some key. To answer this question I need to know if the file is fixed lenght or not, and what the key is. In other words how exactly the input file looks.

@slate (not to hijack the post but...)
Why? I've seen that construction but why is it preferred?

@rrashkin

i used ur 1st code

the file has say 100 numbers(1 t0 100 )

if i search for 1
the output is

Enter number: 1
number found
number found
number found
number found
number found
number found
number found
number found
number found
number found
number found
number found
number found
number found
number found
number found
number found
number found
number found

how can i solve it?
search is returning all 1s including 1s in other numbers

Could you post your exact code here, please? I have a suspicion why it isn't working, but it is hard to say without seeing what is actually running.

i got tht !!!! thanks for your help ,dude

(replaced if <your search string> in line: with '==')

i need binary search python code tht searches a file!!!
compare the each number with search element and display output if number is found

the text file is in sorted order!!

can u provide it??!

Edited 2 Years Ago by victorkvarghese

@slate (not to hijack the post but...)
Why? I've seen that construction but why is it preferred?

Two reasons:

  • It handles the file closing for you
  • You can see on the code where you handle the opened file object. No need to look the space between open and close.

Consider a real (not throwaway or educational) program that opens a file.

The two codes look like:

Without "with":

try:
    f = open(my_file)
    try:
        do_stuff_with_file_object()
    except (Failure) as e:
        handle_application_failure()
except (IOError, OSError) as e:
    do_stuff_when_it_doesnt_work()
finally:
    f.close()

With "with":

try:
    with open(my_file) as f:
        try:
            do_stuff_with_file_object()
        except (Failure) as e:
            handle_application_failure()
except (IOError, OSError) as e:
    do_stuff_when_it_doesnt_work()
This article has been dead for over six months. Start a new discussion instead.