Sponsored By

Sign in to follow this  
shanenin

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')

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this