Skip to content

Commit 626df0f

Browse files
committed
add include_dirs to fileutils.iter_find_files
1 parent cf04eff commit 626df0f

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

boltons/fileutils.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,10 @@ def __exit__(self, exc_type, exc_val, exc_tb):
454454
_CUR_DIR = os.path.dirname(os.path.abspath(__file__))
455455

456456

457-
def iter_find_files(directory, patterns, ignored=None):
458-
"""Returns a generator that yields file paths under a *directory*
459-
(recursively), matching *patterns* using `glob`_ syntax (e.g., ``*.txt``).
460-
Also supports *ignored* patterns.
457+
def iter_find_files(directory, patterns, ignored=None, include_dirs=False):
458+
"""Returns a generator that yields file paths under a *directory*,
459+
matching *patterns* using `glob`_ syntax (e.g., ``*.txt``). Also
460+
supports *ignored* patterns.
461461
462462
Args:
463463
directory (str): Path that serves as the root of the
@@ -466,6 +466,8 @@ def iter_find_files(directory, patterns, ignored=None):
466466
glob-formatted patterns to find under *directory*.
467467
ignored (str or list): A single pattern or list of
468468
glob-formatted patterns to ignore.
469+
include_dirs (bool): Whether to include directories that match
470+
patterns, as well. Defaults to ``False``.
469471
470472
For example, finding Python files in the current directory:
471473
@@ -490,6 +492,14 @@ def iter_find_files(directory, patterns, ignored=None):
490492
ignored = [ignored]
491493
ign_re = re.compile('|'.join([fnmatch.translate(p) for p in ignored]))
492494
for root, dirs, files in os.walk(directory):
495+
if include_dirs:
496+
for basename in dirs:
497+
if pats_re.match(basename):
498+
if ignored and ign_re.match(basename):
499+
continue
500+
filename = os.path.join(root, basename)
501+
yield filename
502+
493503
for basename in files:
494504
if pats_re.match(basename):
495505
if ignored and ign_re.match(basename):

0 commit comments

Comments
 (0)