How can I use pathlib to recursively iterate over all subdirectories of a given directory?
p = Path('docs')
for child in p.iterdir(): child
only seems to iterate over the immediate children of a given directory.
I know this is possible with os.walk()
or glob
, but I want to use pathlib because I like working with the path objects.
Use
Path.rglob
(substitutes the leading**
inPath().glob("**/*")
):You can use the
glob
method of aPath
object:pathlib
hasglob
method where we can provide pattern as an argument.For example :
Path('abc').glob('**/*.txt')
- It will look for current folderabc
and all other subdirectories recursively to locate alltxt
files.