How to use glob() to find files recursively?

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

    I needed a solution for python 2.x that works fast on large directories.
    I endet up with this:

    import subprocess
    foundfiles= subprocess.check_output("ls src/*.c src/**/*.c", shell=True)
    for foundfile in foundfiles.splitlines():
        print foundfile
    

    Note that you might need some exception handling in case ls doesn't find any matching file.

提交回复
热议问题