Recursively iterate through all subdirectories using pathlib

前端 未结 5 1293
清酒与你
清酒与你 2020-12-01 15:36

How can I use pathlib to recursively iterate over all subdirectories of a given directory?

p = Path(\'docs\')
for child in p.iterdir(): child
5条回答
  •  無奈伤痛
    2020-12-01 16:21

    To find just folders the right glob string is:

    '**/'
    

    So to find all the paths for all the folders in your path do this:

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

    If you just want the folder names without the paths then print the name of the folder like so:

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

提交回复
热议问题