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 osdef 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().
Comments
Very neat
Sounds like you're writing a virus ;)
Or using James Orendorff's path module (http://www.jorendorff.com/articles/python/path/):
from path import path
def htmlFiles(): dir = path('C:\\') for fn in dir.walkfiles('*.html'): yield fnGreg: Thanks for the link.
Ruby: Dir["/**/*.html"].each { |file| ... }
I'm sorry, I couldn't help it :)
Charles: surprised you took so long.
That Ruby snippet looks like cartoon-strip swearing. :)
http://www.deftcode.com/archives/every_language_war_ever.html
Bash: find . -name "*mp3" -print