#!/usr/bin/python """ directories.py -- how to see what's in a directory or a directory tree Jeff Ondich, 25 Jan 2008 There are, of course, more file- and directory-related tools. Read the Python documentation for the os and os.path modules. You might also find the glob module helpful. """ import os import sys if len(sys.argv) <= 1: sys.stderr.write('Usage: %s directoryName\n' % sys.argv[0]) sys.exit(1) print '============== listing the contents of a directory ==============' dir = os.listdir(sys.argv[1]) for f in dir: print f print print '============== directory traversal using os.path.walk ==============' def walkerCallback(data, currentDirectory, fileList): print currentDirectory for fileName in fileList: fullPath = os.path.join(currentDirectory, fileName) if os.path.isdir(fullPath): print '\t%s/' % fileName else: print '\t%s' % fileName os.path.walk(sys.argv[1], walkerCallback, '')