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?
To prune the .ipynb_checkpoints
directories from the search, I would use os.walk
.
import os
import fnmatch
from pathlib import Path
file_list = []
for root, subdirs, files in os.walk(rootdir.resolve()):
# Select .py files from the current directory and create Path
file_list.extend([Path(root, file) for file in 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 thedirnames
list in-place (perhaps usingdel
or slice assignment), andwalk()
will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search,...