Search in wildcard folders recursively in python

Hi, I'm trying to do something like

// 1. for x in glob.glob('/../../nodes/*/views/assets/js/*.js'):
// 2 .for x in glob.glob('/../../nodes/*/views/assets/js/*/*.js'):
    print x

is there anything i can do to look for it recursively?

i already looked up Use Glob () to find files recursively in Python? , but os.walk does not accept wildcards, as mentioned above between nodes and views, and http://docs.python.org/library/glob.html docs, which dosent helps a lot.

thank

+5
source share
3 answers

Caution: . Any files matching the template will also be selected anywhere under the root folder, which is the / nodes.

import os, fnmatch

def locate(pattern, root_path):
    for path, dirs, files in os.walk(os.path.abspath(root_path)):
        for filename in fnmatch.filter(files, pattern):
            yield os.path.join(path, filename)

os.walk , , .

js_assets = [js for js in locate('*.js', '/../../nodes')]

locate , .

: glob, glob.

, :

fnmatch.filter( glob.glob('/../../nodes/*/views/assets/js/**/*'), '*.js' )
+9

: fnmatch ` ** `

glob2 formic, easy_install pip.

GLOB2

FORMIC

, : Glob() Python?

glob2 , :

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

, :

parent_path = glob.glob('/../../nodes/*')
for p in parent_path:
    child_paths = glob.glob(os.path.join(p, './views/assets/js/*.js'))
    for c in child_paths:
        #do something

, .

Alternatively, if your environment provides a find command that provides better support for this kind of task. If you are on Windows, there might be a similar program.

+1
source

All Articles