Python os.chown() Method



The Python os.chown() method changes the owner and group id of path to the specified uid and gid. To leave one of the ids unchanged, set the uid or gid to -1. To perform modification operations, you would require super user privilege.

For determining which system resources a user or group can access, Unix-like operating systems identify a user by a value called a user identifier (UID) and identify group by a group identifier (GID).

Note: This method is only available in UNIX/LINUX platforms.

Syntax

Following is the syntax of Python os.chown() method −

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

Parameters

  • path − This is the path for which owner id and group id needs to be setup.

  • uid − This is Owner ID to be set for the file.

  • gid − This is Group ID to be set for the file.

  • dir_fd − A file descriptor pointing to a directory. By default it is None.

  • follow_symlinks − By default it is True. When False, the method will operate on the symlink itself rather than the file to which the link points to.

Note: The * in the argument list means that all subsequent arguments are keyword arguments (dir_fd and follow_symlinks in our case), that is, non-positional arguments.

Return Value

This method does not return any value.

Example

If we does not want to change the gid, we can set it to -1.

The following example shows the usage of Python os.chown() method. Here the ownership of a file path is changed to the given uid '100' and gid '-1'. Hence, only the uid is modified.

import os, sys
# Assuming /tmp/foo.txt exists.
# To set owner ID 100 following has to be done.
os.chown("/tmp/foo.txt", 100, -1)
print ("Changed ownership successfully!!")

When we run above program, it produces following result −

Changed ownership successfully!!

Example

In here, we are trying to change both the uid and gid of the file path. The given uid is '3000' and gid is '2000'.

# importing the module
import os
filepath = "code.txt"
# Printing the current UID and GID of the file
print("The current UID of the file is:", os.stat(filepath).st_uid)
print("The current GID of the file is:", os.stat(filepath).st_gid)
# Using the method
os.chown(path, 3000, 2000)
print("\n The UID and GID of this file has been changed succesfully")
# Printing the UID and GID of the file after modifying
print("\n The modified uid of the file is:", os.stat(filepath).st_uid)
print("The modified gid of the file is:", os.stat(filepath).st_gid)

While executing the above code we get the following output −

sarika@sarika-virtual-machine:~/Desktop$ sudo python3 chown.py
The current UID of the file is: 1000
The current GID of the file is: 1000

 The UID and GID of this file has been changed succesfully

 The modified uid of the file is: 3000
The modified gid of the file is: 2000

Example

By default the follow_symlink is True. When False, the method will operate on the symlink itself rather than the file to which the link points to.

In the example given below a symlink of the given file path "code.txt" is created. Then we are trying to change the uid and gid of the symlink pointing to the file path. Here, the default follow_symlink is used.

import os
filepath = "code.txt"
# creating a symlink for filepath
symlink_path = "code_symlink.txt"
os.symlink(filepath, symlink_path)

# Printing the UID and GID of the filepath and symlink_path
print("The UID of the filepath is:", os.stat(filepath).st_uid)
print("The GID of the filepath is:", os.stat(filepath).st_gid)
print("The UID of the symlink_path is:", os.stat(symlink_path).st_uid)
print("The GID of the symlink_path is:", os.stat(symlink_path).st_gid)

# changing the symlink_path UID and GID of the file with default follow_symlinks
os.chown(symlink_path, 5000, 5000)
# Printing the UID and GID of the filepath and symlink_path
print("changing the symlink_path UID and GID of the file with default follow_symlinks")
print("The UID of the filepath is:", os.stat(filepath).st_uid)
print("The GID of the filepath is:", os.stat(filepath).st_gid)
print("The UID of the symlink_path is:", os.stat(symlink_path).st_uid)
print("The GID of the symlink_path is:", os.stat(symlink_path).st_gid)

Following is an output of the above code −

sarika@sarika-virtual-machine:~/Desktop$ sudo python3 chown.py
The UID of the filepath is: 3000
The GID of the filepath is: 2000
The UID of the symlink_path is: 3000
The GID of the symlink_path is: 2000
changing the UID and GID of the symlink_path file with default follow_symlinks
The UID of the filepath is: 5000
The GID of the filepath is: 5000
The UID of the symlink_path is: 5000
The GID of the symlink_path is: 5000

Example

In here, We can see that when follow_symlinks = False, the execution of os.chown() method only modifies the UID and GID of the symlink file itself. The UID and GID of the linked file have not changed.

import os
filepath = "code.txt"
# creating a symlink for filepath
symlink_path = "code_symlink.txt"
os.symlink(filepath, symlink_path)

# Printing the UID and GID of the path and symlink_path
print("The UID of the path is:", os.stat(filepath).st_uid)
print("The GID of the path is:", os.stat(filepath).st_gid)
print("The UID of the symlink_path is:", os.stat(symlink_path).st_uid)
print("The GID of the symlink_path is:", os.stat(symlink_path).st_gid)

# changing the UID and GID of the symlink_path file with follow_symlinks=False
os.chown(symlink_path, 4000, 4000, follow_symlinks = False)
# Printing the UID and GID of the filepath and symlink_path
print("changing the symlink_path UID and GID of the file with follow_symlinks=False")
print("The UID of the path is:", os.stat(filepath).st_uid)
print("The GID of the path is:", os.stat(filepath).st_gid)
print("The UID of the path_symlink is:", os.stat(symlink_path).st_uid)
print("The GID of the path_symlink is:", os.stat(symlink_path).st_gid)

Output of the above code is as follows −

sarika@sarika-virtual-machine:~/Desktop$ sudo python3 chown.py
The UID of the path is: 5000
The GID of the path is: 5000
The UID of the symlink_path is: 5000
The GID of the symlink_path is: 5000
changing the symlink_path UID and GID of the file with follow_symlinks=False
The UID of the path is: 5000
The GID of the path is: 5000
The UID of the path_symlink is: 5000
The GID of the path_symlink is: 5000
os_file_methods.htm
Advertisements