In fact, those needs have changed over time. Between the third and
fourth editions of this book, the original os.path.walk call was removed
in Python 3.X, and os.walk became the only automated way to perform
tree walks in the standard library. Examples from the prior edition that
used os.path.walk were effectively broken. By contrast, although the
visitor classes used this call, too, its clients did not. Because updating
the visitor classes to use os.walk internally did not alter those classes’
interfaces, visitor-based tools continued to work unchanged.
This seems a prime example of the benefits of OOP’s support for en-
capsulation. Although the future is never completely predictable, in
practice, user-defined tools like visitor tend to give you more control
over changes than standard library tools like os.walk. Trust me on that;
as someone who has had to update three Python books over the last 15
years, I can say with some certainty that Python change is a constant!
Playing Media Files
We have space for just one last, quick example in this chapter, so we’ll close with a bit
of fun. Did you notice how the file extensions for text and binary file types were hard-
coded in the directory search scripts of the prior two sections? That approach works
for the trees they were applied to, but it’s not necessarily complete or portable. It would
be better if we could deduce file type from file name automatically. That’s exactly what
Python’s mimetypes module can do for us. In this section, we’ll use it to build a script
that attempts to launch a file based upon its media type, and in the process develop
general tools for opening media portably with specific or generic players.
As we’ve seen, on Windows this task is trivial—the os.startfile call opens files per
the Windows registry, a system-wide mapping of file extension types to handler pro-
grams. On other platforms, we can either run specific media handlers per media type,
or fall back on a resident web browser to open the file generically using Python’s
webbrowser module. Example 6-23 puts these ideas into code.
Example 6-23. PP4E\System\Media\playfile.py
#!/usr/local/bin/python
"""
##################################################################################
Try to play an arbitrary media file. Allows for specific players instead of
always using general web browser scheme. May not work on your system as is;
audio files use filters and command lines on Unix, and filename associations
on Windows via the start command (i.e., whatever you have on your machine to
run .au files--an audio player, or perhaps a web browser). Configure and
extend as needed. playknownfile assumes you know what sort of media you wish
to open, and playfile tries to determine media type automatically using Python
mimetypes module; both try to launch a web browser with Python webbrowser module
as a last resort when mimetype or platform unknown.
##################################################################################
"""
Playing Media Files | 343