How to use glob() to find files recursively?

前端 未结 28 2441
天涯浪人
天涯浪人 2020-11-21 22:54

This is what I have:

glob(os.path.join(\'src\',\'*.c\'))

but I want to search the subfolders of src. Something like this would work:

<
28条回答
  •  生来不讨喜
    2020-11-21 23:25

    Consider pathlib.rglob().

    This is like calling Path.glob() with "**/" added in front of the given relative pattern:

    import pathlib
    
    
    for p in pathlib.Path("src").rglob("*.c"):
        print(p)
    

    See also @taleinat's related post here and a similar post elsewhere.

提交回复
热议问题