7

hello im 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 can i do to search it recuresively ?

i already looked into Use a Glob() to find files recursively in Python? but the os.walk dont accept wildcards folders like above between nodes and views, and the http://docs.python.org/library/glob.html docs that dosent help much.

thanks

Community
  • 1
  • 1
Adam Ramadhan
  • 22,712
  • 28
  • 84
  • 124

3 Answers3

10

Caveat: This will also select any files matching the pattern anywhere beneath the root folder which is 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)

As os.walk does not accept wildcards we walk the tree and filter what we need.

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

The locate function yields an iterator of all files which match the pattern.

Alternative solution: You can try the extended glob which adds recursive searching to glob.

Now you can write a much simpler expression like:

fnmatch.filter( glob.glob('/../../nodes/*/views/assets/js/**/*'), '*.js' )
Ocaj Nires
  • 3,295
  • 1
  • 18
  • 10
  • 1
    The fnmatch.fiilter() should not be required with glob2: glob.glob('/../../nodes/*/views/assets/js/**/*.js') – miracle2k Apr 01 '12 at 12:26
  • the first solution works for me. As for glob, it worked for listing the files, but when I wanted to split the paths, i didn´t get the required results since the main directory path contain '/' but sub directories have '\\'. (on a windows machine) – M Terry Feb 03 '20 at 23:01
2

I answered a similar question here: fnmatch and recursive path match with `**`

You could use glob2 or formic, both available via easy_install or pip.

GLOB2

FORMIC

You can find them both mentioned here: Use a Glob() to find files recursively in Python?

I use glob2 a lot, ex:

import glob2
files = glob2.glob(r'C:\Users\**\iTunes\**\*.mp4')
Community
  • 1
  • 1
Fnord
  • 5,365
  • 4
  • 31
  • 48
1

Why don't you split your wild-carded paths into multiple parts, like:

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

You can replace some of the above with a list of child assets that you want to retrieve.

Alternatively, if your environment provides the find command, that provides better support for this kind of task. If you're in Windows, there may be an analogous program.

Dana the Sane
  • 14,762
  • 8
  • 58
  • 80