[Python-ideas] Make fnmatch.filter accept a tuple of patterns

Chris Angelico rosuav at gmail.com
Fri Nov 2 14:00:44 EDT 2018

On Sat, Nov 3, 2018 at 4:49 AM Stephen J. Turnbull
<turnbull.stephen.fw at u.tsukuba.ac.jp> wrote:
> Andre Delfino writes:
>  > Frequently, while globbing, one needs to work with multiple extensions. I’d
>  > like to propose for fnmatch.filter to handle a tuple of patterns (while
>  > preserving the single str argument functionality, alas str.endswith),
> This is one of those famous 3-line functions, though:
>     import fnmatch
>     def multifilter(names, *patterns):
>         result = []
>         for p in patterns:
>             result.extend(fnmatch.filter(names, p)
>         return result
> It's a 3-line function in 5 lines, OK, but still.

And like many "hey it's this easy" demonstrations, that isn't quite
identical, as a single file can match multiple patterns (but shouldn't
be in the result multiple times). Whether that's an important
distinction or not remains to be seen, but I do know of situations
where this would have bitten me.


More information about the Python-ideas mailing list