-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
pathlib.Path.unlink raises a NotADirectoryError
if some element of the path is actually a file - this is undocumented
#119993
Comments
I can’t quickly replicate on macOS 14 Intel or macOS 14 ARM locally. There must be something specific about the setup when it happened in cibuildwheel that I’m not capturing. |
You can get a NotADirectoryError if an intermediate path refers to a file. >>> os.unlink('/etc/hosts/file')
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
os.unlink('/etc/hosts/file')
~~~~~~~~~^^^^^^^^^^^^^^^^^^^
NotADirectoryError: [Errno 20] Not a directory: '/etc/hosts/file' |
Thanks ... I just found that through my debugging too... Would it be useful to add a comment to that effect in the docs? |
NotADirectoryError
on macos if the directory does not exist, documentation expects `FileNotFoundErrorNotADirectoryError
if some element of the path is actually a file - this is undocumented
The error comes from the operating system, and it can occur from many This bit is a tiny bit misleading:
It could instead say:
|
That makes sense. I can see how it would quickly become unwieldly to inline all the potential exceptions. How about adjusting the wording to: This method propagates any :exc:`OSError` encountered during removal.
If *missing_ok* is true, :exc:`FileNotFoundError` exceptions will be
ignored (same behavior as the POSIX ``rm -f`` command). This way the reader does not expect to only see a I've pushed this proposal to the PR in case you think it's OK |
On a more general note, as a user of pathlib; the real value in the Concrete Paths is the abstraction they provide over os specifics. This abstraction breaks relatively quickly once errors are encountered, particularly in cases where different OS handle scenarios differently. Would you be interested in adding a short section to the documentation which focusses on this topic as additional help for users? I would be happy to produce a proposal. |
Thinking about it some more... maybe we should treat |
@barneygale, don't worry about "flip-flopping". I often find it's the simple things that are actually hard because of how they slowly highlight subtle things as you work through them. That's a pretty good idea to suppress I'll let that thought rotate subconsciously for a bit and look at the possibility of suppressing |
Given what I just found in testing, I would suggest that treating Windows doesn't raise |
Documentation
https://docs.python.org/3/library/pathlib.html#pathlib.Path.unlink states that a
FileNotFoundError
will be raised if the path does not exist.On macos-14 a
NotADirectoryError
is raised if the directory does not exist.See pypa/cibuildwheel#1850 for an example.
Linked PRs
NotADirectoryError
inPath.unlink()
ifmissing_ok
isTrue
#120049The text was updated successfully, but these errors were encountered: