Python os.access() Method
Example
Try to access file with different mode parameters:
# import os Library
import os
# Check access with os.F_OK
path1 = os.access("text.txt", os.F_OK)
print("Does the path exists:",
path1)
# Check access with os.R_OK
path2 = os.access("text.txt",
os.R_OK)
print("Access to read the file:", path2)
# Check
access with os.W_OK
path3 = os.access("text.txt", os.W_OK)
print("Access to write to file:", path3)
# Check access with
os.X_OK
path4 = os.access("text.txt", os.X_OK)
print("Can path be
executed:", path4)
Run Code »
Definition and Usage
The os.access()
method uses the real uid/gid to check access to the path.
Most operations use effective uid/gid, so this method can be used in a suid/sgid environment to test if the invoking user has specified access to the path.
Syntax
os.access(path, mode, dir_fd, effective_ids, follow_symlinks)
Parameter Values
Parameter | Description |
---|---|
path | Required. Represents the path to be tested for access |
mode | Required. os.F_OK: Checks if the path exists. os.R_OK: Checks if the path is readable. os.W_OK: Checks if the path is writable. os.X_OK: Checks if the path is executable. |
dir_fd | Optional. If NONE, the path will be relative to the directory |
effective_ids | Optional. If TRUE, os.access() will use effective uid/gid instead of the real uid/gid |
follow_symlinks | Optional. If TRUE, os.access() will follow the symbolic link instead of the file the link points to. |
Technical Details
Return Value: | A bool value, TRUE if access is allowed,
otherwise
FALSE |
---|---|
Python Version: | Changed in version 3.6 |
Change Log: | 3.3: Added the dir_fd, effective_ids, and
follow_symlinks parameters. 3.6: Accepts a path-like object. |