Skip to content

Handle not executable directories for os.listdir #117402

Description

@nineteendo

Bug report

Bug description:

I have a directory with "rw-" permisions with a file inaccessible.txt:

>>> import os
>>> os.listdir("directory")
['inaccessible.txt']
>>> os.path.exists("directory/inaccessible.txt")
False

That's inconsistent behaviour: os.listdir says directory/inaccessible.txt exists, while os.path.exists says it does not.
This can be solved simply be raising a PermissionError (or a subclass) for os.listdir when the directory is not executable.
I can't think of any situation outside of a CTF where it would be useful to be able to list the files in such directories.

Note that this is different from a directory with "-wx" permisions with a file secret.txt:

>>> import os
>>> os.listdir("directory")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied: 'directory'
>>> os.path.exists("directory/secret.txt")
True

In this case, files can still be accessed if you know the filename.

CPython versions tested on:

3.12

Operating systems tested on:

macOS

Activity

  1. nineteendo commented on Mar 31, 2024

    @nineteendo
    ContributorAuthor

    @eryksun, what do you think?

  2. nineteendo commented on Mar 31, 2024

    @nineteendo
    ContributorAuthor

    @terryjreedy, this is technically not related to pathlib. Though it would change the behaviour of pathlib.Path.iterdir().

  3. barneygale commented on Mar 31, 2024

    @barneygale
    Contributor

    os.stat() and os.listdir() propagate errors from your operating system rather than raising OSError artificially. This is not a bug, and you'll need to gather support on the ideas forum first if you want to change it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions