How to use glob() to find files recursively?

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

    For python 3.5 and later

    import glob
    
    #file_names_array = glob.glob('path/*.c', recursive=True)
    #above works for files directly at path/ as guided by NeStack
    
    #updated version
    file_names_array = glob.glob('path/**/*.c', recursive=True)
    

    further you might need

    for full_path_in_src in  file_names_array:
        print (full_path_in_src ) # be like 'abc/xyz.c'
        #Full system path of this would be like => 'path till src/abc/xyz.c'
    

提交回复
热议问题