RedKnight91
RedKnight91

Reputation: 400

Python: Recursively search directories containing file with extension, excluding subdirs when file is found

Here's a sample of my directory structure: https://pastebin.com/XimFQdS7

Assuming thousands of subdirectories and files, a recursive search for all files with 'prj' extension could take several seconds.

Assuming I knew that a project dir would only contain one 'pjt' file, I could discard all its subdirs from my search, saving a substantial amount of time.

This would be the desired output for the above structure:

[
    'root/dir1/dirA/',
    'root/dir1/dirB/',
    'root/dir2/',
    'root/dir3/dirA/dirX/',
    'root/dir3/dirA/dirY/'
]

This is my current search code:

def getSubDirectoriesContainingFileType(root, extension):
    os.chdir(root)
    fileFormat = '**/*.{}'.format(extension)
    files = glob.glob(fileFormat, recursive = True)

    matchingDirs = [os.path.dirname(os.path.abspath(file)) for file in files]

    return matchingDirs

I've used glob as I found it to be a bit faster than os.walk() but I think in order to implement the algorithm I'm talking about above I'd have to go back to os.walk().

The idea for the algorithm is:

def searchDirs(root):
    dirs = []

    for dir in rootDirs:
        search for file with ext
        if found:
            append dir to dirs
        else:
            append searchDirs(dir) to dirs
    return dirs

While I could clumsily implement this algorithm, I'm wondering if any of the built-in libraries already provide this functionality, so as to ensure maximum efficiency.

Upvotes: 2

Views: 445

Answers (2)

Mace
Mace

Reputation: 1410

I found your question intriguing so I have done some testing. Comparing the raw result from glob without filtering the unwanted sub directories with a specific search function for the wanted results.

It seems that the specific search is about twice as fast, giving the wanted result without any further processing needed.

I think the trick is

1 immediately stopping the loop if the prj file is found

2 in the same loop simultaneously searching for sub dirs and the prj file.

import os
import random
from pathlib import Path
import time

# --------------------------------------------------------
def make_sub_dirs(dir):

    new_dir_paths = []

    for i in range(4):
        new_dir_path = os.path.join(dir, f'dir{i}')
        os.makedirs(new_dir_path)
        if random.randint(0, 3) == 1:
            prj_file = os.path.join(new_dir_path, 'test.prj')
            Path(prj_file).touch()

        new_dir_paths.append(new_dir_path)

    return new_dir_paths

# --------------------------------------------------------
def make_tree():

    base_dir = [r'K:\test_tree']

    if os.path.exists(base_dir[0]):
        # already made
        return

    current_level_dir_paths = base_dir

    for depth in range(3):
        new_dir_paths = []
        for dir in current_level_dir_paths:
            new_dir_paths.extend(make_sub_dirs(dir))

        current_level_dir_paths = new_dir_paths

# --------------------------------------------------------
def search_first_prj_dirs(search_dir_path):

    dirs_found = []

    dir_entries = os.listdir(search_dir_path)

    for dir_entry in dir_entries:
        dir_entry_path = os.path.join(search_dir_path, dir_entry)
        if os.path.isfile(dir_entry_path):
            if os.path.splitext(dir_entry)[-1] == '.prj':
                # found !!!!!!
                return [search_dir_path]
        else:
            # this should be a dir
            dirs_found.append(dir_entry_path)

    # no prj found, recurse into the found sub dirs
    first_prj_dirs = []
    for dir in dirs_found:
        first_prj_dirs.extend(search_first_prj_dirs(dir))

    return first_prj_dirs

# --------------------------------------------------------

make_tree()

start_time = time.time()
raw_glob_result = list(Path(r'K:\test_tree').rglob('*.prj'))
end_time = time.time()
print('raw glob', len(raw_glob_result), end_time - start_time)

start_time = time.time()
search_first_prj_dirs_result = search_first_prj_dirs(r'K:\test_tree')
end_time = time.time()
print('specific', len(search_first_prj_dirs_result), end_time - start_time)

print('-------------')

raw_glob_result.sort()
for result in raw_glob_result:
    print(result)

print('-------------')

search_first_prj_dirs_result.sort()
for result in search_first_prj_dirs_result:
    print(result)

Result

raw glob 17 0.023003816604614258
specific 11 0.010011434555053711
------------- raw glob
K:\test_tree\dir0\dir0\dir0\test.prj
K:\test_tree\dir0\dir3\dir2\test.prj
K:\test_tree\dir1\dir0\dir2\test.prj
K:\test_tree\dir1\dir0\test.prj
K:\test_tree\dir1\dir2\dir1\test.prj
K:\test_tree\dir1\dir2\dir3\test.prj
K:\test_tree\dir1\dir3\dir0\test.prj
K:\test_tree\dir1\dir3\dir1\test.prj
K:\test_tree\dir1\dir3\test.prj
K:\test_tree\dir2\dir0\dir2\test.prj
K:\test_tree\dir2\dir1\dir1\test.prj
K:\test_tree\dir2\dir2\dir1\test.prj
K:\test_tree\dir2\dir3\dir2\test.prj
K:\test_tree\dir3\dir1\dir1\test.prj
K:\test_tree\dir3\dir2\dir3\test.prj
K:\test_tree\dir3\dir3\test.prj
K:\test_tree\dir3\test.prj
------------- specific
K:\test_tree\dir0\dir0\dir0
K:\test_tree\dir0\dir3\dir2
K:\test_tree\dir1\dir0
K:\test_tree\dir1\dir2\dir1
K:\test_tree\dir1\dir2\dir3
K:\test_tree\dir1\dir3
K:\test_tree\dir2\dir0\dir2
K:\test_tree\dir2\dir1\dir1
K:\test_tree\dir2\dir2\dir1
K:\test_tree\dir2\dir3\dir2
K:\test_tree\dir3

Upvotes: 2

deadshot
deadshot

Reputation: 9071

Using Path.rglob

from pathlib import Path

def searchDirs(root):
    return list(Path(root).rglob('*.prj'))

Output:

/root/dir1/dirB/project.prj
/root/dir1/dirB/test/project.prj
/root/dir1/diraA/project.prj
/root/dir3/dirx/project.prj
/root/dir2/project.prj

Upvotes: 0

Related Questions