Copying Directory Trees
The next three sections conclude this chapter by exploring a handful of additional utilities for processing directories (a.k.a. “folders”) on your computer with Python. They present directory copy, deletion, and comparison scripts that demonstrate system tools at work. All of these were born of necessity, are generally portable among all Python platforms, and illustrate Python development concepts along the way.
Some of these scripts do something too unique for the
visitor
module’s classes we’ve been
applying in early sections of this chapter, and so require more
custom solutions (e.g., we can’t remove directories we intend
to walk through). Most have platform-specific equivalents too (e.g.,
drag-and-drop copies), but the Python utilities shown here are
portable, easily customized, callable from other scripts, and
surprisingly fast.
A Python Tree Copy Script
My CD writer sometimes does weird things. In fact, copies of files with odd names can be totally botched on the CD, even though other files show up in one piece. That’s not necessarily a show-stopper -- if just a few files are trashed in a big CD backup copy, I can always copy the offending files to floppies one at a time. Unfortunately, Windows drag-and-drop copies don’t play nicely with such a CD: the copy operation stops and exits the moment the first bad file is encountered. You only get as many files as were copied up to the error, but no more.
There may be some magical Windows setting to work around ...
Get Programming Python, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.