Python os.access() Method


Description

Python method access() uses the real uid/gid to test for access to path. 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.It returns True if access is allowed, False if not.

Syntax

Following is the syntax for access() method −

os.access(path, mode);

Parameters

Return Value

This method returns True if access is allowed, False if not.

Example

The following example shows the usage of access() method.

#!/usr/bin/python

import os, sys

# Assuming /tmp/foo.txt exists and has read/write permissions.

ret = os.access("/tmp/foo.txt", os.F_OK)
print "F_OK - return value %s"% ret

ret = os.access("/tmp/foo.txt", os.R_OK)
print "R_OK - return value %s"% ret

ret = os.access("/tmp/foo.txt", os.W_OK)
print "W_OK - return value %s"% ret

ret = os.access("/tmp/foo.txt", os.X_OK)
print "X_OK - return value %s"% ret

When we run above program, it produces following result −

F_OK - return value True
R_OK - return value True
W_OK - return value True
X_OK - return value False
python_files_io.htm