[Python-ideas] find-like functionality in pathlib

Brendan Moloney moloney at ohsu.edu
Mon Dec 28 18:53:33 EST 2015


Not sure how useful this is, but I ended up writing my own "pythonic find" module: https://github.com/moloney/pathmap/blob/master/pathmap.py

I was mostly worried about minimizing stat calls, so I used scandir rather than Pathlib.

The only documentation is the doc strings, but the basic idea is you can have one "matching" rule and any number of ignore/prune rules.  The rules can be callables or strings that are treated as regular expressions (I suppose it might be better if the default was to treat strings as glob expressions instead...).

So for the original use case that spawned this thread, you would do something like:

pm = PathMap(prune_rules=['/\.git$'])
for match in pm.matches(['path/to/some/dir']):
    if not match.dir_entry.is_dir():
        print(match.path)

Or if you wanted to do something similar but only print names of python modules it would be something like:

pm = PathMap('.+/(.+)\.py$', prune_rules=['/\.git$'])
for match in pm.matches(['path/to/some/dir']):
  if not match.dir_entry.is_dir():
        print(match.match_info[1])

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151228/83e249e0/attachment.html>


More information about the Python-ideas mailing list