How to use glob() to find files recursively?

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

    import os, glob
    
    for each in glob.glob('path/**/*.c', recursive=True):
        print(f'Name with path: {each} \nName without path: {os.path.basename(each)}')
    
    • glob.glob('*.c') :matches all files ending in .c in current directory
    • glob.glob('*/*.c') :same as 1
    • glob.glob('**/*.c') :matches all files ending in .c in the immediate subdirectories only, but not in the current directory
    • glob.glob('*.c',recursive=True) :same as 1
    • glob.glob('*/*.c',recursive=True) :same as 3
    • glob.glob('**/*.c',recursive=True) :matches all files ending in .c in the current directory and in all subdirectories

提交回复
热议问题