# os.scandir should have an option to also yield the path itself

**URL:** <https://discuss.python.org/t/os-scandir-should-have-an-option-to-also-yield-the-path-itself/18383>\
**Category:** Ideas\
**Created:** [August 22, 2022, 3:27am UTC](https://discuss.python.org/t/os-scandir-should-have-an-option-to-also-yield-the-path-itself/18383 "2022-08-22T03:27:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![calestyo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/calestyo/32/8584_2.png) [@calestyo](https://discuss.python.org/u/calestyo)\
**Post date:** [August 22, 2022, 3:27am UTC](https://discuss.python.org/t/os-scandir-should-have-an-option-to-also-yield-the-path-itself/18383/1 "2022-08-22T03:27:41Z")

</div>

I think it would be nice if `os.scandir` had an option (that defaults to the current behaviour) which makes it also yield the root path itself as given by its `path` argument.

The benefit would be, that if one has code like e.g.:

```auto
def scandirtree(path):
    for p in os.scandir(path):
        if p.is_dir(follow_symlinks=False):
            yield p
            yield from scandirtree(p)
        else:
            yield p

```

one would also yield `path`, an process that as well (in whatever one does with the generator.

Cheers,  
Chris.

---

<div class="post-metadata">

**Author:** ![EpicWink](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/epicwink/32/17968_2.png) [@EpicWink](https://discuss.python.org/u/EpicWink)\
**Post date:** [August 22, 2022, 4:55am UTC](https://discuss.python.org/t/os-scandir-should-have-an-option-to-also-yield-the-path-itself/18383/2 "2022-08-22T04:55:16Z")

</div>

Your example is similar to [Recursive directory list with pathlib.Path.iterdir · Issue #80783 · python/cpython · GitHub](https://github.com/python/cpython/issues/80783)

---

<div class="post-metadata">

**Author:** ![calestyo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/calestyo/32/8584_2.png) [@calestyo](https://discuss.python.org/u/calestyo)\
**Post date:** [August 22, 2022, 1:27pm UTC](https://discuss.python.org/t/os-scandir-should-have-an-option-to-also-yield-the-path-itself/18383/3 "2022-08-22T13:27:19Z")

</div>

I made numerous tests recently, and `os.scandir()` seem to be by far the fastest way to traverse. So that’s why I’d basically would want to see it in there.

Actually, I forgot to write the motivation for the whole thing above:  
IMO, a typical use case when traversing a directory tree is something like:  
`rm -rf foo` or `find foo`

The usual behaviour for all these is always to also include the specified directory itself.

Now if one considers my above `scandirtree()` function and maybe makes this a bit more complicated like not crossing mountpoints (as with `find`’s `-xdev` option) or already `stat()`ing some stuff and returning that, too.

If this shall then be used in some code as in:

```auto
for p in scandirtree(root_path):
   ...

```

then whatever I do there (both, the additional logic in `scandirtree()` and what I do with the results in the outer for loop), I need to fully repeat for `root_path`.

An alternative approach (but not sure whether that would break any other expectations on iterators or PathLike objects) would be if one could manually create `os.DirEntry` objects, then one could write the above as:

```auto
def scandirtree(path):
    yield os.DirEntry(path)
    for p in os.scandir(path):
        if p.is_dir(follow_symlinks=False):
            yield from scandirtree(p)
        else:
            yield p

```

Another (unrelated) idea would be to let `os.scandir()` work on non-directory files as `path`, i.e. os.scandir(“/path/to/regular-file”) would simply return an iterator that yields nothing but the file itself.

That would IMO further simplify usage, cause right now, when I have pathnames as arguments, and want to loop over them (any directories recursively), I need to add a manual check, when it’s a dir, use the `os.scandir()` respectively my `scandirtree()` and if not, again, re-do everything which I’d already do in `scandirtree()` and where I use that, for a single file.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [August 22, 2022, 11:53pm UTC](https://discuss.python.org/t/os-scandir-should-have-an-option-to-also-yield-the-path-itself/18383/4 "2022-08-22T23:53:01Z")

</div>

> [@](#):
>
> An alternative approach (but not sure whether that would break any other expectations on iterators or PathLike objects) would be if one could manually create `os.DirEntry` objects

Why can’t we create `os.DirEntry` objects directly?

---

<div class="post-metadata">

**Author:** ![eryksun](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/eryksun/32/697_2.png) [@eryksun](https://discuss.python.org/u/eryksun)\
**Post date:** [August 23, 2022, 2:38am UTC](https://discuss.python.org/t/os-scandir-should-have-an-option-to-also-yield-the-path-itself/18383/5 "2022-08-23T02:38:29Z")

</div>

> [@steven.daprano](#):
>
> Why can’t we create `os.DirEntry` objects directly?

The internal constructors work with low-level OS data (e.g. a `WIN32_FIND_DATAW` record in Windows). `DirEntry` caches this data to implement `is_dir()`, `is_file()`, `is_symlink()`, `inode()` in POSIX, and `stat(follow_symlinks=False)` in Windows.

When PEP 471 was first approved in 2014, `os.DirEntry` wasn’t exposed. It was [added in 2016](https://github.com/python/cpython/issues/71225), but nothing was done to support a generic constructor that would create an instance from a path and cache the `stat()` result. There’s an [open issue](https://github.com/python/cpython/issues/71983) to implement this capability.

---

<div class="post-metadata">

**Author:** ![calestyo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/calestyo/32/8584_2.png) [@calestyo](https://discuss.python.org/u/calestyo)\
**Post date:** [November 14, 2022, 2:17am UTC](https://discuss.python.org/t/os-scandir-should-have-an-option-to-also-yield-the-path-itself/18383/6 "2022-11-14T02:17:48Z")

</div>

Just for the records:  
Since there was no clear outcome here, I’ve just requested it at [os.scandir(): add option to yielt the given path itself. · Issue #99454 · python/cpython · GitHub](https://github.com/python/cpython/issues/99454) .

Thanks,  
Chris
