[Python-Dev] os.access versus os.exist

"Martin v. Löwis" martin at v.loewis.de
Mon Nov 15 22:58:41 CET 2004


Robert Brewer wrote:
> It would be awfully nice (on posix platforms, for my use-case) to find
> out whether a file is inaccessible due to permission restrictions, or
> due to non-existence.

That's easy enough: just check the exception you get when you try
opening the file.

Don't trust that a negative answer from access() means that you cannot
open the file, or that a positive answer means that you can. In short,
the implementation of access(2) might lie to you. For example, you might
have write access, but writing fails because you are over quota. Or,
you are running setuid, and the effective uid is different from the
real one.

> Can we add an optional "error_if_not_found" arg to
> os.access? Make a
> new os function with the desired behavior?

Either would be fine. Changing the return value would not, since
you would have to negate the result.

> The next issue, then, is other platforms: is similar functionality
> available in OS/2, unix, etc.?

On Unix, access(2) can return (in errno) these errors:
EACCES, ELOOP, ENAMETOOLONG, ENOENT, ENOTDIR, EROFS, and, optionally,
EFAULT, EINVAL, EIO, ENOMEM, ETXTBSY.

Regards,
Martin


More information about the Python-Dev mailing list