SoFunction
Updated on 2025-03-01

Python usage examples

Overview

The () method uses the current uid/gid to try to access the path. Most operations use valid uid/gid, so the running environment can be tried in the suid/sgid environment.

grammar

The syntax format of the access() method is as follows:

(path, mode);

parameter

  • path -- The path to detect whether there is access permission.
  • mode -- mode is F_OK, tests the path existing, or it can be one or more of R_OK, W_OK and X_OK or R_OK, W_OK and X_OK.
  • os.F_OK: As the mode parameter of access(), test whether the path exists.
  • os.R_OK: Included in the mode parameter of access() to test whether the path is readable.
  • os.W_OK is included in the mode parameter of access() to test whether the path is writable.
  • os.X_OK is included in the mode parameter of access() to test whether the path is executable.

Return value

Return True if access is allowed, otherwise return False.

Example

The following example demonstrates the use of the access() method:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# Assuming the /tmp/ file exists and has read and write permissions

ret = ("/tmp/", os.F_OK)
print "F_OK - Return value %s"% ret

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

ret = ("/tmp/", os.W_OK)
print "W_OK - Return value %s"% ret

ret = ("/tmp/", os.X_OK)
print "X_OK - Return value %s"% ret

The output result of executing the above program is:

F_OK - Return value True
R_OK - Return value True
W_OK - Return value True
X_OK - Return value False