Search code examples
pythondirectory-structure

Is there existing any utilities to get all absolute path of leaf file under a given directory?


os.listdir(path) can return all the files under the directory of the given path. I an wonder if there exists any utility function that can conduct deep traverse and return all the paths of leaf files under a given directory.


Solution

  • Sure! You are looking for os.walk:

    import os
    for root, dirs, files in os.walk(path):
        for filename in files:
            filename = os.path.join(root, filename)
            print(filename)