Searching Directory Trees
Engineers love to change things. As I was writing this book, I found it almost irresistible to move and rename directories, variables, and shared modules in the book examples tree, whenever I thought I’d stumbled on to a more coherent structure. That was fine early on, but as the tree became more intertwined, this became a maintenance nightmare. Things like program directory paths and module names were hardcoded all over the place -- in package import statements, program startup calls, text notes, configuration files, and more.
One way to repair these references, of course, is to edit every file
in the directory by hand, searching each for information that has
changed. That’s so tedious as to be utterly impossible in this
book’s examples tree, though; as I wrote these words, the
example tree contained 118 directories and 1342 files! (To count for
yourself, run a command-line python
PyTools/visitor.py
1 in the
PP2E examples root directory.) Clearly, I needed
a way to automate updates after changes.
Greps and Globs in Shells and Python
There is a standard way to search files for strings on Unix and Linux
systems: the command-line program grep and its
relatives list all lines in one or more files containing a string or
string pattern.[43] Given that Unix shells expand (i.e.,
“glob”) filename patterns automatically, a command such
as grep
popen
*.py will search a single directory’s Python files for string “popen”. Here’s such a command in action on Windows (I ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access