''' 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: print('Usage: {0} directory_name'.format(sys.argv[0]), file=sys.stderr) exit() print('============== listing the contents of a directory ==============') directory = os.listdir(sys.argv[1]) for f in directory: print(f) print() print('============== directory traversal using os.walk ==============') for current_directory, subdirectory_list, file_list in os.walk(sys.argv[1]): print(current_directory) for filename in file_list: full_path = os.path.join(current_directory, filename) if os.path.isdir(full_path): print('\t%s/' % filename) else: print('\t%s' % filename)