How to use glob() to find files recursively?

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

    Starting with Python 3.4, one can use the glob() method of one of the Path classes in the new pathlib module, which supports ** wildcards. For example:

    from pathlib import Path
    
    for file_path in Path('src').glob('**/*.c'):
        print(file_path) # do whatever you need with these files
    

    Update: Starting with Python 3.5, the same syntax is also supported by glob.glob().

提交回复
热议问题