How to use glob() to find files recursively?

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

    pathlib.Path.rglob

    Use pathlib.Path.rglob from the the pathlib module, which was introduced in Python 3.5.

    from pathlib import Path
    
    for path in Path('src').rglob('*.c'):
        print(path.name)
    

    If you don't want to use pathlib, use can use glob.glob('**/*.c'), but don't forget to pass in the recursive keyword parameter and it will use inordinate amount of time on large directories.

    For cases where matching files beginning with a dot (.); like files in the current directory or hidden files on Unix based system, use the os.walk solution below.

    os.walk

    For older Python versions, use os.walk to recursively walk a directory and fnmatch.filter to match against a simple expression:

    import fnmatch
    import os
    
    matches = []
    for root, dirnames, filenames in os.walk('src'):
        for filename in fnmatch.filter(filenames, '*.c'):
            matches.append(os.path.join(root, filename))
    

提交回复
热议问题