fixname = os.path.normcase(thisDir)
if fixname in visited:
if trace: tryprint('skipping ' + thisDir)
else:
visited.add(fixname)
for filename in filesHere:
if filename.endswith(extname):
if trace > 1: tryprint('+++' + filename)
fullname = os.path.join(thisDir, filename)
try:
bytesize = os.path.getsize(fullname)
linesize = sum(+1 for line in open(fullname, 'rb'))
except Exception:
print('error', exc_info()[0])
else:
allsizes.append((bytesize, linesize, fullname))
for (title, key) in [('bytes', 0), ('lines', 1)]:
print('\nBy %s...' % title)
allsizes.sort(key=lambda x: x[key])
pprint.pprint(allsizes[:3])
pprint.pprint(allsizes[-3:])
Unlike the prior tree version, this one allows us to search in specific directories, and
for specific extensions. The default is to simply search the current working directory
for Python files:
C:\...\PP4E\System\Filetools> bigext-tree.py
.
By bytes...
[(21, 1, '.\\__init__.py'),
(461, 17, '.\\bigpy-dir.py'),
(818, 25, '.\\bigpy-tree.py')]
[(1696, 48, '.\\join.py'),
(1940, 49, '.\\bigext-tree.py'),
(2547, 57, '.\\split.py')]
By lines...
[(21, 1, '.\\__init__.py'),
(461, 17, '.\\bigpy-dir.py'),
(818, 25, '.\\bigpy-tree.py')]
[(1696, 48, '.\\join.py'),
(1940, 49, '.\\bigext-tree.py'),
(2547, 57, '.\\split.py')]
For more custom work, we can pass in a directory name, extension type, and trace level
on the command-line now (trace level 0 disables tracing, and 1, the default, shows
directories visited along the way):
C:\...\PP4E\System\Filetools> bigext-tree.py .. .py 0
By bytes...
[(21, 1, '..\\__init__.py'),
(21, 1, '..\\Filetools\\__init__.py'),
A Quick Game of “Find the Biggest Python File” | 277