#!/usr/bin/env python import os, os.path """ reportdir is the directory on the filesystem in which to write the text file w/ results fsbranch is the directory that you want to investigate all the way downstream - the last directory in this string will be the name of the text file """ reportdir = '/Users/garyp/Desktop/' # include trailing slash fsbranch = '/Users/garyp/Desktop/WWW' # trailing slash not necessary, but won't hurt lines = [] def findFiles(arg,dir,files): a = os.path.abspath(dir) a = a.replace(fsbranch, ' ') # excludes fsbranch part of absolute path in report lines.append('\n###'+a+'\n') if files: files.sort() for file in files: size = '' fullpath = '%s/%s' % (dir, file) if os.path.isfile(fullpath): size = os.path.getsize(fullpath) if size: lines.append('%s >> %s' % (file, size)) else: lines.append(file) fname = '%s%s.txt' % (reportdir, fsbranch.split('/')[-1].lower()) file = open(fname, 'w') for line in lines: file.write(line+'\n') file.close() os.path.walk(fsbranch, findFiles, '')