Has Path from pathlib a function which can check subfolders?

Question:

I want to find all python files in a folder which are not located in a subfolder (e.g. '.ipynb_checkpoints').

My current solution is

from pathlib import Path

rootdir = Path('/home/my_path')
# For absolute paths instead of relative the current dir
file_list = [
    f for f in rootdir.resolve().glob('**/*.py')
    if not '.ipynb_checkpoints' in str(f)
]

which gives me the correct list.

Neverteheless I was hoping that pathlib has some function like f.includes() or something similar.

Is there a soltion to generate the same list only using the functions of the pathlib package?

Asked By: mosc9575

||

Answers:

To prune the .ipynb_checkpoints directories from the search, I would use os.walk.

file_list = []
for root, subdirs, files in os.walk(rootdir.resolve()):
    # Select .py files from the current directory
    file_list.extend(fnmatch.filter(files, '*.py'))

    # In-place removal of checkpoint directories to prune them
    # from the walk.
    subdirs[:] = filter(lambda x: x != ".ipynb_checkpoints", subdirs)

From os.walk:

When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search,…

Answered By: chepner
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.