Python vs. Ruby (and os.path.walk)

Matt Gerrans mgerrans at
Fri Aug 9 09:55:35 CEST 2002

I hate to (well, not really!  ;-) spoil all your scripting fun with a swift
chop of the proverbial Gordian Knot, but you might want to look first to the
native capabilities of your operating system shell, such as it is.   This
will do the trick in a lot fewer lines of code than any other scripting

cd /d d:\path\target
del *_i.c, *_p.c *.thi *.thl /s /f

(You only need the /f if there will be read-only files in the bunch)

To find out how fast it is, do this:
timethis del *_i.c, *_p.c,*.thi, *.thl /s /f

To test the speed of this I created a directory tree like so:
C:\temp\junk>for /l %f in (1,1,100) do @for /l %g in (1,1,10) do @md

And populated it with files like so:
C:\temp\junk>echo This is a file>a.txt
C:\temp\junk>for /l %f in (1,1,100) do @for /l %g in (1,1,10) do for %i in
(_i.c,_p.c,.thi,.thl,.obj,.cpp) do @copy \temp\junk\a.txt
basedir%f\subdir%g\%f_stuff %i

(Yes the *creation* of this sample data is pretty ugly, but the deleting is
clean and simple)

I think this created around 6000 files to sift through (4000 of which were
subsequently deleted).  Doing the recursive delete above took about 11
seconds (it less than 9 when the output was redirected to a log file).

Don't get me wrong, I love Python and use it for all kinds of things, but
this is a case where no scripting language can compete with the OS's command
shell in simplicity or speed.

- Matt

More information about the Python-list mailing list