How can I use pathlib to recursively iterate over all subdirectories of a given directory?
p = Path(\'docs\')
for child in p.iterdir(): child
Use list comprehensions:
(1) [f.name for f in p.glob("**/*")] # or
(2) [f.name for f in p.rglob("*")]
You can add if f.is_file()
or if f.is_dir()
to (1) or (2) if you want to target files only or directories only, respectively. Or replace "*"
with some pattern like "*.txt"
if you want to target .txt
files only.
See this quick guide.