python 查找多个目录下的最大Python文件 脚本

2022-05-13 11:08:21 浏览数 (2)

""" 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]) pprint.pprint(allsizes[:3]) pprint.pprint(allsizes[-3:])

0 人点赞