Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

search in wildcard folders recursively in python

Tags:

python

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

like image 654
Adam Ramadhan Avatar asked Aug 08 '11 18:08

Adam Ramadhan


People also ask

How do I find a directory recursively in Python?

Using Glob() function to find files recursively We can use the function glob. glob() or glob. iglob() directly from glob module to retrieve paths recursively from inside the directories/files and subdirectories/subfiles.

Which are the wildcard characters for finding a file folder?

There are two wildcard characters: asterisk (*) and question mark (?). Asterisk (*) - Use the asterisk as a substitute for zero or more characters. e.g., friend* will locate all files and folders that begin with friend. Question mark (?)- Use the question mark as a substitute for a single character in a file name.

How do I read all files in a directory in Python?

To get a list of all the files and folders in a particular directory in the filesystem, use os. listdir() in legacy versions of Python or os. scandir() in Python 3.


2 Answers

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' )
like image 174
Ocaj Nires Avatar answered Sep 20 '22 22:09

Ocaj Nires


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')
like image 33
Fnord Avatar answered Sep 20 '22 22:09

Fnord