Python os.chmod() Method

Python os.chmod() method is used to change the mode of the file or directory to the numeric mode, including permissions such as read, write, and execute.

This mode value can be defined using symbolic constants found in the stat module.

Syntax

os.chmod(path, mode, *, dir_fd=None, follow_symlinks=True)

Parameters

Name Description
path (string) It is a path to the file or directory.
mode (int) The new mode (permissions) you want to set. The stat module provides constants like:

stat.S_ISUID – It sets the user ID on execution
stat.S_ISGID – It sets group ID on execution
stat.S_ENFMT – Record locking enforced
stat.S_ISVTX – Save text image after execution
stat.S_IREAD – Read by owner
stat.S_IWRITE – Write by owner
stat.S_IEXEC – Execute by owner
stat.S_IRWXU – Read, write, and execute by owner
stat.S_IRUSR – Read by owner
stat.S_IWUSR – Write by owner
stat.S_IXUSR – Execute by owner
stat.S_IRWXG – Read, write, and execute by group
stat.S_IRGRP – Read by group
stat.S_IWGRP – Write by group
stat.S_IXGRP – Execute by group
stat.S_IRWXO – Read, write, and execute by others
stat.S_IROTH – Read by others
stat.S_IWOTH – Write by others
stat.S_IXOTH – Execute by others

dir_fd (optional) It is a file descriptor that refers to a directory.
follow_symlinks(optional, boolean) If True, symbolic links are followed, and the mode is changed on the link’s target. If False, the mode is changed on the symbolic link itself.

Return value

This method does not return any value.

Important points

  1. Permissions: The permissions include read (r), write (w), and execute (x) for the owner, group, and others.
  2. Symbolic and numeric modes: Modes can be specified using symbolic representations (like ‘rwxr-xr-x’) or octal numbers (like 0o755).
  3. Cross-Platform considerations: File permission mechanisms differ between Unix/Linux and Windows. On Windows, only the read-only flag can be set or cleared (using stat.S_IWRITE and stat.S_IREAD).

Example 1: Basic usage

Screenshot of file information before using os.chmod() function

Now, we will change the permissions of this “data.txt” file.

import os

# Setting the file to read and write by the owner
os.chmod('data.txt', 0o600)

Output

Output of os.chmod() method in Python

Example 2: Using symbolic constants

import os
import stat

# Making a file readable, writable, and executable by the owner
os.chmod('data.txt', stat.S_IRWXU)

Output

Output of using symbolic constants

Example 3: Changing the mode of a directory

Before changing the mode of a directory:

Before changing the permissions of a directory using os.chmod() method

import os

# Setting a directory to be accessible 
# (read, write, execute) only by the owner

os.chmod('dir1', 0o700)

After changing the mode of a directory:

After changing the mode of a directory using Python os.chmod() method

Example 4: Error handling

import os

try:
  os.chmod('nonexistent_file.txt', 0o600)
except FileNotFoundError as error:
  print(f"Error: {error}")

Output

Error: [Errno 2] No such file or directory: 'nonexistent_file.txt'

That’s all!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.