Skip to content

os.scandir(): add option to yield the given path itself #99454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
calestyo opened this issue Nov 14, 2022 · 1 comment
Open

os.scandir(): add option to yield the given path itself #99454

calestyo opened this issue Nov 14, 2022 · 1 comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@calestyo
Copy link
Contributor

Feature or enhancement

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.:

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 and process that as well (in whatever one does with the generator).

It doesn't seem to be possible right now to write a warpper around scandir which would just yield path first, since one cannot create os.DirEntry objects (see #71983).

Pitch

The above in turn would be the familiar behaviour of many tools, e.g.

$ rm -rf path

also removes the path itself, not just its contents.

find path

also finds path itself.
Etc. pp.

So anyone who wants to implement a tool like these or similar behaviour, would no longer need to specially handle the path itself.

Previous discussion

https://discuss.python.org/t/os-scandir-should-have-an-option-to-also-yield-the-path-itself/18383

Thanks,
Chris.

@calestyo calestyo added the type-feature A feature request or enhancement label Nov 14, 2022
@bland328
Copy link

bland328 commented Apr 7, 2023

I have the very same need. I've written an os.scandir() wrapper capable of returning the root directory, but as I'm unable to construct an os.DirEntry object to represent it (#71983), I have to return my own PseudoDirEntry object in that one case, which is a messy solution.

An optional new os.scandir() behavior to return path would be welcome, or I'd happily settle for the ability to instantiate os.DirEntry.

In the meanwhile, does anyone know of an ugly hack that would persuade the existing os.DirEntry implementation to provide an object for an arbitrary path?

@calestyo calestyo changed the title os.scandir(): add option to yielt the given path itself. os.scandir(): add option to yield the given path itself Apr 7, 2023
@arhadthedev arhadthedev added the stdlib Python modules in the Lib dir label Apr 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants