This Python Search Script Is Fast


Recommended Posts

I have been messing around with searching for files on a computer(using python). I wanted to search for all files on my c drivce that end with '.avi'. this little script found them in 16 seconds. A similar search with windows search feature took 50 seconds.

import os

for i in os.walk('c:/'):
   for j in i[2]:
       if j.endswith('.avi'):
           base = os.path.abspath(i[0])
           print "%s\%s"%(base, j)
           
raw_input('enter retrun to exit')

Link to post
Share on other sites

Search is a bit odd. It seems to do a depth-first search under Documents and Settings and breadth-first, with no obvious ordering, everywhere else. But not quite. It almost makes sense. Could just be because of what I'm searching for, I guess.

It doesn't surprise me that your script is faster. It would surprise me a bit if it was consistently faster.

Edited by jcl
Link to post
Share on other sites

I tried to do something different, it still seems quite faster. I searched for all files containing the string 'the' in the titile, and are more then 1mb in size. To keep it even I had windows search all hidden and system folders also. this python script did it in 18 seconds, the windows search feature did it in 48 seconds.

import os

def pysearch(word):
   for i in os.walk('c:/'):
       for j in i[2]:
           if j.rfind(word) != -1:
               base = os.path.abspath(i[0])
               full_path = "%s\%s"%(base, j)
               if os.path.getsize(full_path) >= 1024000:
                   print full_path
               
               
pysearch('the')

raw_input('enter retrun to exit')

edit added later//

the windows search may be doing some other useful(or bloatful) stull that the python search is not doing.

Edited by shanenin
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...