Change Directory Permissions Using Python



In Python, Modifying the permission of a directory can be done using the subprocess module and, the chmod() Function ( of the os module).

Using 'subprocess' Module

The subprocess module in Python provides various functions to create a new (child) process and establish a connection to the I/O devices.

This module has a function named call(),  This function helps us to run the shell commands of the underlying Operating system. You just need to pass the respective command with the desired options (in the recommended order).

For example, the command to set permissions read-write permissions to a user in the Unix Operating System is as follows -

chmod -R +w <directory_name>

Following is the syntax of the call() method to execute this command -

subprocess.call(['chmod', '-R', '+w', 'my_folder'])

Where, 

  • chmod: Using this command you can modify the file/directory permissions in the underlying operating system.
  • -R: This is the flag that tells the chmod command to apply the permission changes recursively.
  • +w mode: This is the option of the chmod command which denotes the "read and write" permission. It allocates read and write permissions to the user on the specified directory.

Example

Following is a Python example to set read and write permissions to a directory using the subprocess module -

import subprocess
subprocess.call(['chmod', '-R', '+w', 'my_folder'])

Using the 'os' module

We can also set permissions to a directory using the chmod() function of the os module. Following is the syntax ?

os.chmod(path, mode);

This will take two arguments as follows ?

  • Path: The path to the file or directory of which permission we want to change.
  • Mode: The desired permission is expressed as an octal number.

Generating the Path

Before setting the permissions first of all we need to browse through the desired directory, you can do so, using the os.walk() method. The functionality of the os.walk() Method is the same as generating the file names in a directory by walking the tree either from top-down or bottom-up, which means it starts from the files and then moves up to the root.

The os.path.join(root, d) function helps you to join two directories and create a path.

Example

Following is the example of setting permissions to a directory using the chmod() method here we are creating a recursive function that accepts path and permission mode as parameters. Within this function, we are ?

  • Using the walk() function to generate the path of the given directory, here, we are adding all the directories to the path from rot to the given folder.
  • Using the chmod() method to set the specified permissions (as per the given mode parameter) to the desired directory.
  • While calling this function we are providing the directory name and the octal number that indicates the read-write mode (0o777) as parameters.
import os
#Changing the permissions of all files and directories Recursively
def change_permissions_recursive(path, mode):   
    
   # Traverse the directory tree starting from 'path' to top
   for root, dirs, files in os.walk(path, topdown=False):
	
      # Iterate over the directories in the 'root' directory
      for dir in [os.path.join(root, d) for d in dirs]:
         os.chmod(dir, mode)
        
      # Iterate over the files in the 'root' directory
      for file in [os.path.join(root, f) for f in files]:
         # Change the permissions of each file
         os.chmod(file, mode)

# Calling the function 
change_permissions_recursive('my_folder', 0o777)
Updated on: 2024-09-23T16:50:06+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements