What are all the ways to traverse directory trees?

directory-traversal, filesystems, language-agnostic

Solution

In Python:

If you're looking for a quick, clean, and portable solution try:

import os
base_dir = '.'

def foo(arg, curr_dir, files):
  print curr_dir
  print files

os.path.walk(base_dir, foo, None)

Note that you can modify foo to do something else instead of just printing the names. Furthermore, if you're interested in migrating to Python 3.0, you will have to use os.walk() instead.

Problem

How do you traverse a directory tree in your favorite language? What do you need to know to traverse a directory tree in different operating systems? On different filesystems? What's your favorite library/module for aiding in traversing a directory tree?

Original source