Most of these are almost as trivial as the visitor_edit.py code in Example 6-19, because
the visitor framework handles walking details automatically. The collector, for in-
stance, simply appends to a list as a search visitor detects matched files and allows the
default list of text filename extensions in the search visitor to be overridden per
instance—it’s roughly like a combination of find and grep on Unix:
>>> from visitor_collect import CollectVisitor
>>> V = CollectVisitor('mimetypes', testexts=['.py', '.pyw'], trace=0)
>>> V.run(r'C:\temp\PP3E\Examples')
>>> for name in V.matches: print(name) # .py and .pyw files with 'mimetypes'
...
C:\temp\PP3E\Examples\PP3E\Internet\Email\mailtools\mailParser.py
C:\temp\PP3E\Examples\PP3E\Internet\Email\mailtools\mailSender.py
C:\temp\PP3E\Examples\PP3E\Internet\Ftp\mirror\downloadflat.py
C:\temp\PP3E\Examples\PP3E\Internet\Ftp\mirror\downloadflat_modular.py
C:\temp\PP3E\Examples\PP3E\Internet\Ftp\mirror\ftptools.py
C:\temp\PP3E\Examples\PP3E\Internet\Ftp\mirror\uploadflat.py
C:\temp\PP3E\Examples\PP3E\System\Media\playfile.py
C:\...\PP4E\Tools> visitor_collect.py mimetypes C:\temp\PP3E\Examples # as script
The core logic of the biggest-file visitor is similarly straightforward, and harkens back
to chapter start:
class BigPy(FileVisitor):
def __init__(self, trace=0):
FileVisitor.__init__(self, context=[], trace=trace)
def visitfile(self, filepath):
FileVisitor.visitfile(self, filepath)
if filepath.endswith('.py'):
self.context.append((os.path.getsize(filepath), filepath))
And the bytecode-removal visitor brings us back full circle, showing an additional al-
ternative to those we met earlier in this chapter. It’s essentially the same code, but it
runs os.remove on “.pyc” file visits.
In the end, while the visitor classes are really just simple wrappers for os.walk, they
further automate walking chores and provide a general framework and alternative class-
based structure which may seem more natural to some than simple unstructured loops.
They’re also representative of how Python’s OOP support maps well to real-world
structures like file systems. Although os.walk works well for one-off scripts, the better
extensibility, reduced redundancy, and greater encapsulation possible with OOP can
be a major asset in real work as our needs change and evolve over time.
342 | Chapter 6: Complete System Programs