Next / Previous / Contents / Shipman's homepage

7.3. report(): Generate one directory tree's report

The report() function generates the portion of the report for one directory subtree. The path name to the subtree is its argument.

bigfiles.py
# - - -   r e p o r t   - - -

def report ( dirName ):
    """Generate the report for one directory subtree.

      [ dirName is a string ->
          sys.stdout  +:=  a report listing the files below
              directory (dirName), with files in descending
              order by size ]
    """

This function has only three steps: write a report heading; instantiate the BigReport object containing the report data; and call that object's .genFiles() method to generate the lines of the report.

Each report starts with a line showing the name of the subtree's starting directory. This uses Python's os.path.realpath() function, which resolves soft links and relative path names to the actual absolute path name.

We also set basePath to the absolute path name corresponding to dirName. This is necessary to the BigReport object so that it can display each file's path name relative to that base directory. For this, we use the os.path.abspath() function, which does not replace soft links with their real locations.

bigfiles.py
    #-- 1 -
    # [ basePath  :=  dirName's absolute path name
    #   sys.stdout  +:=  report heading showing dirName's real
    #                    absolute path ]
    basePath  =  os.path.abspath ( dirName )
    print "\n   === %s ===" % os.path.realpath ( dirName )

    #-- 2 --
    # [ bigReport  :=  a BigReport object describing all the
    #       accessible files in directory tree (dirName) ]
    bigReport  =  BigReport ( basePath )

    #-- 3 --
    # [ bigReport is a BigReport object ->
    #     sys.stdout  +:=  lines describing files in bigReport
    #         in descending order by size ]
    for  bigInfo in bigReport.genFiles():
        print bigInfo