Python os.access() Method

  1. Syntax of the os.access() Method
  2. Example 1: Working With the os.access() Method in Python
  3. Example 2: Use Conditional Statement With the os.access() Method in Python
  4. Example 3: Write Into File After Using the os.access() Method in Python

Python os.access() method is an efficient way of using the OS-dependent functionality. This method uses the uid/gid (user identifier/group identifier) to test for access to a path/folder.

Syntax of the os.access() Method

os.access(path, mode)

Parameters

path It is an address object of a file system path or a symlink. The path is used for existence, validity, or existence mode.
mode The mode tells which characteristics of the path we are checking.
To test the existence of a path, we use os.F_OK.
To test the readability of a path, we use os.R_OK.
To test if the path is writeable or not, we use the mode os.W_OK.
The last mode is os.X_OK, which is used to determine if the path can be executed or not.

Return

The return type of this method is a Boolean value. It returns the bool value true if access is granted; otherwise, false is returned.

Example 1: Working With the os.access() Method in Python

import os

directory1 = os.access("file.txt", os.F_OK)
print("Does the path exist? :", directory1)

directory2 = os.access("file.txt", os.R_OK)
print("Access to read the file granted :", directory2)

directory3 = os.access("file.txt", os.W_OK)
print("Access to write the file granted :", directory3)

directory4 = os.access("file.txt", os.X_OK)
print("Is the path executable?:", directory4)

Output:

Does the path exist? : False
Access to read the file granted : False
Access to write the file granted : False
Is the path executable?: False

os.access() may not work on some operating systems. You can try writing import sys at the start of the code to include the required libraries per your system requirement.

Example 2: Use Conditional Statement With the os.access() Method in Python

Use the os.access() method to check if a user is authorized to open a file before it is made available.

import os

if os.access("file.txt", os.R_OK):
    with open("file.txt") as file:
        a = file.read()
        print(a)
else:
    print("An error has occurred.")

Output:

An error has occurred.

Example 3: Write Into File After Using the os.access() Method in Python

import os

file_name = "file.txt"

if os.access(file_name, os.X_OK):
    f1 = open(file_name, 'w')
    f1.write('something\n')
    f1.close()
else:
    print ("You don't have read permission.")

Output:

You don't have read permission.

I/O file operations may fail even when os.access() returns true because some network file systems may have permissions semantics beyond the usual POSIX permission-bit model.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python OS

  • Python os.chdir() Method
  • Python os.closerange() Method
  • Python os.path.getsize() Method