How to use glob() to find files recursively?

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

    import sys, os, glob
    
    dir_list = ["c:\\books\\heap"]
    
    while len(dir_list) > 0:
        cur_dir = dir_list[0]
        del dir_list[0]
        list_of_files = glob.glob(cur_dir+'\\*')
        for book in list_of_files:
            if os.path.isfile(book):
                print(book)
            else:
                dir_list.append(book)
    

提交回复
热议问题