Skip to content

Handle not executable directories for os.listdir #117402

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

Closed
Tracked by #117361
nineteendo opened this issue Mar 31, 2024 · 3 comments
Closed
Tracked by #117361

Handle not executable directories for os.listdir #117402

nineteendo opened this issue Mar 31, 2024 · 3 comments
Labels
topic-pathlib type-bug An unexpected behavior, bug, or error

Comments

@nineteendo
Copy link
Contributor

nineteendo commented Mar 31, 2024

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

@nineteendo nineteendo added the type-bug An unexpected behavior, bug, or error label Mar 31, 2024
@nineteendo nineteendo mentioned this issue Mar 31, 2024
16 tasks
@nineteendo
Copy link
Contributor Author

@eryksun, what do you think?

@nineteendo
Copy link
Contributor Author

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

@barneygale
Copy link
Contributor

barneygale commented Mar 31, 2024

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.

@barneygale barneygale closed this as not planned Won't fix, can't repro, duplicate, stale Mar 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-pathlib type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants