Quick: what’s the biggest Python source file on your computer? This was the query innocently posed by a student in one of my Python classes. Because I didn’t know either, it became an official exercise in subsequent classes, and it provides a good example of ways to apply Python system tools for a realistic purpose in this book. Really, the query is a bit vague, because its scope is unclear. Do we mean the largest Python file in a directory, in a full directory tree, in the standard library, on the module import search path, or on your entire hard drive? Different scopes imply different solutions.
For instance, Example 6-1 is a first-cut solution that looks for the biggest Python file in one directory—a limited scope, but enough to get started.
Example 6-1. PP4E\System\Filetools\bigpy-dir.py
""" Find the largest Python source file in a single directory. Search Windows Python source lib, unless dir command-line arg. """ import os, glob, sys dirname = r'C:\Python31\Lib' if len(sys.argv) == 1 else sys.argv allsizes =  allpy = glob.glob(dirname + os.sep + '*.py') for filename in allpy: filesize = os.path.getsize(filename) allsizes.append((filesize, filename)) allsizes.sort() print(allsizes[:2]) print(allsizes[-2:])
This script uses the
module to run through a directory’s files and detects the largest by storing sizes and names on a list that is sorted at the end—because size appears first in ...