Python os.chown() Method

Python os.chown() method is used to change the owner and group ID of the provided path or directory path to the provided numeric owner ID (UID) and group ID (GID).

To change the owner of a file, you must have appropriate privileges (typically root access).

This method is completely relevant in Unix or Unix-like systems. On Windows, it has limited use and may not behave as expected.

This operation is specifically sensitive and should be used cautiously, as improper use can affect system security and file accessibility. The user and group IDs (uid and gid) must be valid on the system where the script is running.

Syntax

os.chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)

Parameters

Name Description
path (string) The path to the file or directory.
uid (int) The user id (UID) to set. It’s an integer representing the user and is specific to the system.
gid (int) The group ID (GID) to set. Similar to UID, it’s an integer representing the group.
dir_fd (optional) A file descriptor refers to a directory.
follow_symlinks (optional, boolean) If True, symbolic links are followed, and the owner and group of the target are changed. If False, the owner and group of the symbolic link itself are changed.

Return value

This method does not return any value. The behavior of this method with symbolic links depends on the follow_symlinks argument.

Example 1: Basic usage

import os

# Change the owner and group of a file
# Here, we are using example user and group ids (1000 and 1000)
os.chown('data.txt', 1000, 1000)
print("\nOwner and group id of the file has been changed!") 

print("\nOwner id of the file:", os.stat('data.txt').st_uid)
print("Group id of the file:", os.stat('data.txt').st_gid)

Output

Output of os.chown() function in Python

Example 2: Changing the owner and group of a directory

import os

# Change the owner and group of a directory
os.chown('dir1', 1000, 1000)

print("\nOwner and group id of a directory has been changed!")

print("\nOwner id of the directory:", os.stat('dir1').st_uid)
print("Group id of the directory:", os.stat('dir1').st_gid)

Output

Output of os.chown() method on directory

Example 3: Not following symbolic links

import os

# Change the owner and group of the symbolic link itself, not the target
os.chown('linked-file', 1000, 1000, follow_symlinks=False)

print("\nOwner and group id of a symlink has been changed!")

print("\nOwner id of the symlink:", os.stat('linked-file').st_uid)
print("Group id of the symlink:", os.stat('linked-file').st_gid)

Output

Output of using os.chown() method on symlinks

Example 4: Error handling

import os

try:
  # Change the owner and group of the non_existent text file
  os.chown('non_existent_file.txt', 1000, 1000, follow_symlinks=False)

  print("\nOwner and group id of a non existent text file has been changed!")

  print("\nOwner id of the symlink:", os.stat('non_existent_file.txt').st_uid)
  print("Group id of the symlink:", os.stat('non_existent_file.txt').st_gid)

except FileNotFoundError as error:
  print(f"Error: {error}")

Output

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

That’s all!

Leave a Comment

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