os.walk()

1 PM February 23, 2004

This morning I was writing a script that operates on every html file on my hard disk. I was just going to have my script read the output of ”find . -name ’*.html’”, but then I remembered Python 2.3’s os.walk() function. Here’s the code to find my html files:

import os

def htmlFiles():
for (path, dames, fnames) in os.walk(“C:\\”):
for fn in fnames:
if fn.endswith(”.html”):
yield os.path.join(path, fn)

Very neat.

Update: Added the import and the os.path.join().

By alang | # | Comments (7)
(Posted to Software Development and Python)
© 2003-2006 Alan Green