Fnmatch and recursive path match with `**`

Is there any built-in or easy way to recursively match paths with a double asterisk, for example. for example zsh does?

For example, when

path = 'foo/bar/ham/spam/eggs.py'

I can use fnmatch to check it with

fnmatch(path, 'foo/bar/ham/*/*.py'

Although, I would like to be able to:

fnmatch(path, 'foo/**/*.py')

I know that fnmatch matches its pattern with a regular expression , so in the case of words, I can collapse my own fnmatch with an additional pattern **but maybe there is an easier way

+4
source share
2 answers

fnmatch, * .* , , / - UNIX:

while i < n:
    c = pat[i]
    i = i+1
    if c == '*':
        res = res + '.*'
    elif c == '?':
        res = res + '.'
    elif c == '[':
        ...

,

>>> fnmatch.fnmatch('a/b/d/c', 'a/*/c')
True
>>> fnmatch.fnmatch('a/b/d/c', 'a/*************c')
True
+3

os.walk, :

glob2

formic

glob2:

import glob2
files = glob2.glob(r'C:\Users\**\iTunes\**\*.mp4')

:

Python 3.5 glob :

import glob
files = glob.iglob(r'C:\Users\**\iTunes\**\*.mp4', recursive=True) 
+3

All Articles