from __future__ import generators # needed for Python 2.2 import sys def walktree(top = ".", depthfirst = True): """Walk the directory tree, starting from top. Credit to Noah Spurrier and Doug Fort.""" import os, stat, types names = os.listdir(top) if not depthfirst: yield top, names for name in names: try: st = os.lstat(os.path.join(top, name)) except os.error: continue if stat.S_ISDIR(st.st_mode): for (newtop, children) in walktree (os.path.join(top, name), depthfirst): yield newtop, children if depthfirst: yield top, names def makeHTMLtable(top, depthfirst=False): from xml.sax.saxutils import escape # To quote out things like & ret = ['\n'] for top, names in walktree(top): ret.append(' \n'%escape(top)) for name in names: ret.append(' \n'%escape(name)) ret.append('
%s
%s
') return ''.join(ret) # Much faster than += method def makeHTMLpage(top, depthfirst=False): return '\n'.join(['', '' '', ' Search results', ' ', '', '', '

Search Results

', makeHTMLtable(top, depthfirst), '', '']) if __name__ == '__main__': if len(sys.argv) == 2: top = sys.argv[1] else: top = '.' print makeHTMLpage(top)