How to change the permission of a directory 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 functions to create new processes and execute shell commands. The call() function allows us to run operating system commands directly from Python.

For Unix-based systems, the command to set read-write permissions is ?

chmod -R +w <directory_name>

Syntax

import subprocess
subprocess.call(['chmod', 'options', 'mode', 'directory_name'])

Where ?

  • chmod − Command to modify file/directory permissions
  • -R − Flag for recursive permission changes
  • +w − Grant write permission
  • +r − Grant read permission
  • +x − Grant execute permission

Example

Setting read and write permissions recursively ?

import subprocess
import os

# Create a test directory
os.makedirs('test_folder', exist_ok=True)

# Set read and write permissions recursively
result = subprocess.call(['chmod', '-R', '+rw', 'test_folder'])

if result == 0:
    print("Permissions changed successfully")
else:
    print("Failed to change permissions")

Using os Module

The os.chmod() function provides a more direct approach to change permissions using octal notation.

Syntax

import os
os.chmod(path, mode)

Parameters ?

  • path − File or directory path
  • mode − Permission mode as octal number

Common Permission Modes

Octal Permission Description
0o755 rwxr-xr-x Owner: read/write/execute, Others: read/execute
0o777 rwxrwxrwx All users: read/write/execute
0o644 rw-r--r-- Owner: read/write, Others: read only

Example

Changing permissions for a single directory ?

import os

# Create a test directory
os.makedirs('sample_dir', exist_ok=True)

# Change permissions to read/write/execute for owner, read/execute for others
os.chmod('sample_dir', 0o755)

# Check current permissions
import stat
mode = os.stat('sample_dir').st_mode
print(f"Current permissions: {oct(stat.S_IMODE(mode))}")

Recursive Permission Changes

For recursive permission changes using os module ?

import os

def change_permissions_recursive(path, mode):
    """Change permissions recursively for all files and directories."""
    
    # Change permission of the root directory first
    os.chmod(path, mode)
    
    # Walk through all subdirectories and files
    for root, dirs, files in os.walk(path):
        
        # Change permissions for directories
        for directory in dirs:
            dir_path = os.path.join(root, directory)
            os.chmod(dir_path, mode)
            
        # Change permissions for files
        for file in files:
            file_path = os.path.join(root, file)
            os.chmod(file_path, mode)

# Create test structure
os.makedirs('test_dir/subdir', exist_ok=True)
with open('test_dir/test_file.txt', 'w') as f:
    f.write('Test content')

# Apply permissions recursively
change_permissions_recursive('test_dir', 0o755)
print("Permissions changed recursively")

Comparison

Method Platform Best For Complexity
subprocess Unix/Linux Shell command familiarity Low
os.chmod() Cross-platform Direct Python control Medium

Conclusion

Use os.chmod() for cross-platform compatibility and direct Python control. Use subprocess when you prefer shell command syntax. Both methods effectively change directory permissions, with os.chmod() being more portable across different operating systems.

Updated on: 2026-03-24T17:25:22+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements