ID EN
OS

access

Python 3.11

Use the real uid/gid to test for access to path. Note that most operations will use the effective uid/gid, therefore this routine can be used in a suid/sgid environment to test if the invoking user has the specified access to path. mode should be F_OK to test the existence of path, or it can be the inclusive OR of one or more of R_OK, W_OK, and X_OK to test permissions. Return True if access is allowed, False if not. See the Unix man page access(2) for more information.

Syntax

PYTHON
os.access(path, mode, *, dir_fd=None, effective_ids=False, follow_symlinks=True)

Examples

Example 1
PYTHON
if os.access("myfile", os.R_OK):
    with open("myfile") as fp:
        return fp.read()
return "some default data"
Example 2
PYTHON
try:
    fp = open("myfile")
except PermissionError:
    return "some default data"
else:
    with fp:
        return fp.read()

See Also

F_OK R_OK W_OK X_OK True False paths relative to directory descriptors not following symlinks