VOOZH about

URL: https://www.geeksforgeeks.org/python/python-os-access-method/

⇱ Python | os.access() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python | os.access() Method

Last Updated : 11 Jul, 2025

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).

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."

Output:

Facing some issue
Comment