Open In App

Python | os.access() Method

Last Updated : 11 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

os.access() method uses the real uid/gid to test for access to the path. Most operations 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 the path. In this article, we will learn about the access() function of the OS module in Python.

os.access() Function Syntax in Python

Syntax: os.access(path, mode)

Parameters:

  • path: path to be tested for access or existence mode: Should be F_OK to test the existence of path, or can be the inclusive OR of one or more of R_OK, W_OK, and X_OK to test permissions.

Following values can be passed as the mode parameter of access() to test the following:

  • os.F_OK: Tests existence of the path.
  • os.R_OK: Tests readability of the path.
  • os.W_OK: Tests writability of the path.
  • os.X_OK: Checks if path can be executed.

  Returns: True if access is allowed, else returns False.

Python os.access() Method Example

Below are some examples by which we can understand how to determine if a file is readable using os.R_OK in Python and how to verify if a file is writable with os.W_OK in Python:

Checking File Access Permission Using os.access()

In this example, the code uses the os.access() function to check different access permissions for a file named “gfg.txt”. It checks the file’s existence (os.F_OK), read permission (os.R_OK), write permission (os.W_OK), and execution permission (os.X_OK).

Python3




# importing all necessary libraries
import os
import sys
 
# Checking access with os.F_OK
path1 = os.access("gfg.txt", os.F_OK)
print("Exists the path:", path1)
 
# Checking access with os.R_OK
path2 = os.access("gfg.txt", os.R_OK)
print("Access to read the file:", path2)
 
# Checking access with os.W_OK
path3 = os.access("gfg.txt", os.W_OK)
print("Access to write the file:", path3)
 
# Checking access with os.X_OK
path4 = os.access("gfg.txt", os.X_OK)
print("Check if path can be executed:", path4)


Output:

Exists the path: True
Access to read the file: True
Access to write the file: False
Check if path can be executed: False

Open a File and Validating Access Using os.access() Function

In this example, the code checks if the file “gfg.txt” has read (`os.R_OK`) access. If readable, it opens the file and returns its content using a with statement to automatically handle file closure. If the file is not readable, it returns the string “Facing some issue.”

Python3




# checking readability of the path
if os.access("gfg.txt", os.R_OK):
 
    # open txt file as file
    with open("gfg.txt") as file:
        return file.read()
 
# in case can't access the file
return "Facing some issue"


Output:

Facing some issue

FAQ’s

Python – Difference between os.access and os.path.exists?

In Python, `os.path.exists()` checks if a file or directory exists at a given path, returning a boolean. Meanwhile, `os.access()` assesses specific permissions, allowing verification of read, write, or execute permissions on a file. The former is for existence, while the latter offers more detailed access control.

Meaning of ‘f’ in the os.F_OK mode in the os.access() method?

In the os.access() method, the ‘f’ in the os.F_OK mode stands for “file.” When used as an argument to os.access(), os.F_OK checks for the existence of the specified file path. It is part of a set of constants representing different modes, and specifically, os.F_OK verifies if the file is present at the specified location, returning True if it exists and False otherwise.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads