[Tutor] Hard drive searching

Bruce Sass bsass@freenet.edmonton.ab.ca
Tue, 30 Jan 2001 12:58:58 -0700 (MST)


> I've been thinking about this for awhile, but can't figure anything out. What I need my python program to do is, go through my hard drive, as quickly as possible, and return all the files that end with a certain extension(like .html or .py). How could I do this?

If the Python glob module doesn't do it for you...

I wouldn't do it.  Python is just too slow for that kinda thing, IMHO.
It may work ok on a modern system, but if someone tries it on an
old/slow box it will be excruciating.

This is what I would do with my old/slow Linux box...
os.system("updatedb")  # only if running as the superuser
os.system("locate .ext > list-of-files-with-.ext-in-their-path")
potential_files_of_type_ext = open("list-of-files...their-path", "r")

"updatedb" updates the system's database of files, only really useful to 
the superuser in this case because only the superuser can see the entire
system.  "locate .ext" spits out very path that includes ".ext"
somewhere in it, the results would need to be filtered through a regex.
The unix "find" command would be better, but its use is more version
dependent (a PITA to make portable).  ">" redirects stdout to a file.

How would this be done on a Windows, Mac, ...?  Are there any
collections of code snippets that compare the various ways of doing, in
a Python program, the stuff that is best left to the native OS/kernel?