How to use glob() to find files recursively?

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

    For python >= 3.5 you can use **, recursive=True :

    import glob
    for x in glob.glob('path/**/*.c', recursive=True):
        print(x)
    

    Demo


    If recursive is True, the pattern ** will match any files and zero or more directories and subdirectories. If the pattern is followed by an os.sep, only directories and subdirectories match.

提交回复
热议问题