[Python-ideas] Adding '**' recursive search to glob.glob (original) (raw)

Vinay Sajip vinay_sajip at yahoo.co.uk
Mon Jan 14 16:46:29 CET 2013


Paul Moore <p.f.moore at ...> writes:

I'd like it if the glob module supported the (relatively common) facility to use ** to mean recursively search subdirectories. It's a reasonably straightforward patch, and offers a feature that is fairly difficult to implement in user code on top of the existing functionality. The syntax is supported in a number of places (for example the bash shell and things like Java Ant) so it will be relatively familiar to users.

Agreed. This was in packaging/distutils2 and I have now got it in distlib [1]; it supports both recursive globs and variants using the {opt1,opt2,op3} syntax.

One obvious downside is that if used carelessly, it can make globbing pretty slow. So I'd propose that it be added as an optional extension enabled using a flag argument (glob(pat, allowrecursive=True)) which is false by default. That would also mean that backward compatibility should not be an issue.

Isn't the requirement to recurse implied by the presence of '' in the pattern? What's to be gained by specifying it using allow_recursive as well? Will having allow_recursive=True have any effect if '' is not in the pattern? If you specify a pattern with '' and allow_recursive=False, does that mean that '' effectively acts as '*' would (i.e. one directory level only)?

Regards,

Vinay Sajip

[1] https://bitbucket.org/vinay.sajip/distlib/src/29666/distlib/glob.py?at=default



More information about the Python-ideas mailing list