On Tue, 14 Jul 2026 at 02:25, Rob Cliffe via Python-list
<[email protected]> wrote:
> It seems to me to be useful to distinguish between
>      cases where the attempt fails because the file is owned by someone else
>      the case where the attempt only fails because the file is read-only.
> We already have a FileExistsError subclass of OSError, which AIUI allows
> you to distinguish
> between the cases where trying to create an file (say)
>      fails because the file already exists
>      fails because you do not have permission to write to the parent
> directory.
> This distinction is useful information.

The question is, HOW do you distinguish? FileExistsError is not a
variant form of PermissionsError; it, as per the documentation,
corresponds to the EEXIST error return. OSError subclasses tell you
about different errno values. Prior to FileNotFoundError's
introduction in Python 3.3 (along with a number of others), the way to
recognize that a file wasn't found was:

except OSError as e:
    if e.errno != errno.ENOENT: raise
    # it's "File Not Found", proceed accordingly

This is very different from what you're asking for, which is a
separate and independent check, and introduces TOCTOU issues.

> In the same way, surely it is useful to have more information
> about why trying to delete a file failed.

Only if the OS provides it.

> I was talking about a read-only file, not a read-only directory.
> That may be another ball game - one I haven't thought about.
> (I wasn's suggesting a DirectoryReadOnly Exception, though there might
> be some merit in the idea.)

Yes, and on Linux, a read-only file can be removed without error. So
the distinction is important. That's why the error is simply "you
aren't allowed to do that", without trying to crystal-ball the reason
for that.

> You jest of course.  My point was that such logic may be written not
> just by me,
> but by many others.  Reinventing the wheel.

I'm unconvinced that it is, though. I've never implemented that logic,
and among those who have, I think you'll find that there are subtly
different variations for different situations. You're specifically
wanting to check the *file's* permissions, not its containing
directory, and not whether the file system *as a whole* is read only.
That's correct for your situation, but not for everyone's.

ChrisA
-- 
https://mail.python.org/mailman3//lists/python-list.python.org

Reply via email to