How to use glob() to find files recursively?

前端 未结 28 1842
天涯浪人
天涯浪人 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:31

    Simplified version of Johan Dahlin's answer, without fnmatch.

    import os
    
    matches = []
    for root, dirnames, filenames in os.walk('src'):
      matches += [os.path.join(root, f) for f in filenames if f[-2:] == '.c']
    

提交回复
热议问题