[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

Scanning the Module Search Path


Sure enough—the prior section’s script found smallest and largest files in subdirecto-
ries. While searching Python’s entire standard library tree this way is more inclusive,
it’s still incomplete: there may be additional modules installed elsewhere on your com-
puter, which are accessible from the module import search path but outside Python’s
source tree. To be more exhaustive, we could instead essentially perform the same tree
search, but for every directory on the module import search path. Example 6-3 adds
this extension to include every importable Python-coded module on your computer—
located both on the path directly and nested in package directory trees.


Example 6-3. PP4E\System\Filetools\bigpy-path.py


"""
Find the largest Python source file on the module import search path.
Skip already-visited directories, normalize path and case so they will
match properly, and include line counts in pprinted result. It's not
enough to use os.environ['PYTHONPATH']: this is a subset of sys.path.
"""


import sys, os, pprint
trace = 0 # 1=dirs, 2=+files


visited = {}
allsizes = []
for srcdir in sys.path:
for (thisDir, subsHere, filesHere) in os.walk(srcdir):
if trace > 0: print(thisDir)
thisDir = os.path.normpath(thisDir)
fixcase = os.path.normcase(thisDir)
if fixcase in visited:
continue
else:
visited[fixcase] = True
for filename in filesHere:
if filename.endswith('.py'):
if trace > 1: print('...', filename)
pypath = os.path.join(thisDir, filename)
try:
pysize = os.path.getsize(pypath)
except os.error:
print('skipping', pypath, sys.exc_info()[0])
else:
pylines = len(open(pypath, 'rb').readlines())
allsizes.append((pysize, pylines, pypath))


print('By size...')
allsizes.sort()
pprint.pprint(allsizes[:3])
pprint.pprint(allsizes[-3:])


print('By lines...')
allsizes.sort(key=lambda x: x[1])


274 | Chapter 6: Complete System Programs

Free download pdf