Search code examples
pythonscandiryield-from

How to use os.scandir() to also get back empty and non-empty directory names


In https://stackoverflow.com/a/33135143 the solution for returning recursively all file names in a directory structure looks like depicted below.

I need also the information about each sub directory in the directory structure and the full path name of the files and directories. So if I have this structure:

ls -1 -R
.:
a
b

./a:
fileC

./b:

I would need:

/a
/b
/a/fileC

How do I have to change the solution from the answer stated above to achieve this? For the sake of completion, the answer is given below:

try:
    from os import scandir
except ImportError:
    from scandir import scandir  # use scandir PyPI module on Python < 3.5

def scantree(path):
    """Recursively yield DirEntry objects for given directory."""
    for entry in scandir(path):
        if entry.is_dir(follow_symlinks=False):
            yield from scantree(entry.path)  # see below for Python 2.x
        else:
            yield entry

if __name__ == '__main__':
    import sys
    for entry in scantree(sys.argv[1] if len(sys.argv) > 1 else '.'):
        print(entry.path)

Solution

  • You should yield the current entry whether or not it's a directory. If it's a directory, you also recurse to get the contents.

    def scantree(path):
        """Recursively yield DirEntry objects for given directory."""
        for entry in scandir(path):
            yield entry
            if entry.is_dir(follow_symlinks=False):
                yield from scantree(entry.path)