Search code examples
pythonfindglobsubdirectory

Python globbing to match find's "-print -quit" behavior


I have a few arbitrarily deep directories, each of which contains a single file with a consistent name. In the command line I can use find <dir> -name <filename> -print -quit for optimized searching: once it finds the file, it stops looking through the directory. I can do that because it's found exactly what I'm looking for.

I can use glob (or os.walk, I suppose) to do the same thing. But neither of these options seem to have a way to stop once the file I'm looking for is found: both index the full directory regardless - globbing looks for as many matches as possible, and os.walk will only allow me to filter after the index is complete.

Is there a way to get that optimized find behavior in Python, short of doing a find in a subprocess?


Solution

  • If you use the topdown=True option to os.walk(), which is the default, it doesn't generate the entire hierarchy first; the generator returns one directory at a time. This allows you to change the dirlist value in place to prevent it from descending into some subdirectories, and also allows you to break out of the loop early.