I know how to use os.walk().

I know how to implement all I need with it.

Nevertheless I think things could be simpler.

For simple stuff I use the command line tool find, since it is declarative.

If I use os.walk() I do it the imperative way.

Example: to include only files which end with '.txt' I use this in Python:

if not file.endswith('.txt'):

The find way is much easier:

find -name '*.txt'

Is there a library which provides a declarative way to traverse a directory tree with python?

  • 1
    It is ok if you down-vote the question. But I would like to know why you do it. Please leave a comment. What is wrong with my question?
    – guettli
    Commented Nov 8, 2015 at 16:14

3 Answers 3


There is the grin python library which includes grind.

  • grin is a python implementation of grep using python regular expression syntax and being python it is cross platform
  • grind is a python implementation of find.

Both can be used as libraries or command line utilities.

Grin command line usage:

>grind --help
usage: grind-script.py [-h] [-v] [-s] [--skip-hidden-files] [-b]
                       [--skip-backup-files] [-S] [--skip-hidden-dirs]
                       [-d SKIP_DIRS] [-D] [-e SKIP_EXTS] [-E] [--no-follow]
                       [--follow] [-0] [--dirs DIRS [DIRS ...]] [--sys-path]

Find text and binary files using similar rules as grin.

positional arguments:
  glob                  the glob pattern to match; you may need to quote this
                        to prevent the shell from trying to expand it

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -s, --no-skip-hidden-files
                        do not skip .hidden files
  --skip-hidden-files   do skip .hidden files
  -b, --no-skip-backup-files
                   do not skip backup~ files [deprecated; edit --skip-
 --skip-backup-files   do skip backup~ files [default] [deprecated; edit
 -S, --no-skip-hidden-dirs
                       do not skip .hidden directories
 --skip-hidden-dirs    do skip .hidden directories
 -d SKIP_DIRS, --skip-dirs SKIP_DIRS
                       comma-separated list of directory names to skip
 -D, --no-skip-dirs    do not skip any directories
 -e SKIP_EXTS, --skip-exts SKIP_EXTS
                       comma-separated list of file extensions to skip [defau
 -E, --no-skip-exts    do not skip any file extensions
 --no-follow           do not follow symlinks to directories and files
 --follow              follow symlinks to directories and files
 -0, --null-separated  print the filenames separated by NULs
 --dirs DIRS [DIRS ...]
                       the directories to start from
 --sys-path            search the directories on sys.path
  • Thank you for this link. The tool seem to focus on the command line usage.
    – guettli
    Commented Nov 11, 2015 at 14:50

python-glob2 is an extention of glob that support recursive '**' globbing syntax. It can do the job if you are not looking for tons of special options

>>> import glob2
>>> all_header_files = glob2.glob('src/**/*.h')
['src/fs.h', 'src/media/mp3.h', 'src/media/mp3/frame.h', ...]

There is also the library python-fs which allows syntax like:

files = fs.find('*.txt')

which returns a generator or you could use:

filelists = list(fs.find('*.txt'))

Unfortunately it has a name clash with py-filesystem which I find much more useful personally.

  • 1
    A name clash ... not nice. I created an issue in the projects with less commits and less contributors: github.com/chaosmail/python-fs/issues/4
    – guettli
    Commented Nov 24, 2015 at 9:06
  • Also the tool with, IMHO, less value added - user friendly but not a lot that you can't do with the built in libraries in python-fs while py-filesystem rocks. Commented Nov 25, 2015 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.