Recursively iterate through all subdirectories using pathlib Recursively iterate through all subdirectories using pathlib python python

Recursively iterate through all subdirectories using pathlib


Use Path.rglob (substitutes the leading ** in Path().glob("**/*")):

path = Path("docs")for p in path.rglob("*"):     print(p.name)


You can use the glob method of a Path object:

p = Path('docs')for i in p.glob('**/*'):     print(i.name)


pathlib has glob method where we can provide pattern as an argument.

For example : Path('abc').glob('**/*.txt') - It will look for current folder abc and all other subdirectories recursively to locate all txt files.