Python os.chown(path, uid, gid)
method is used to change the owner and group id of path to the numeric uid and gid.
- To leave one of the ids unchanged, set it to -1
- Requires superuser privileges to perform the modification operation
- os.chown() method is only available on UNIX/LINUX platforms
os.chown Syntax
os.chown(path, uid, gid, *, dir_fd = None, follow_symlinks = True)
os.chown Parameters
- path – File path for setting permissions
- uid – User ID
- gid – User group ID
- dir_fd (Optional) – FD pointing to a directory
- follow_symlinks (Optional) – Bool value, default is True. when False, the method will operate on the symbolic link itself and not on the file to which the link points.
The
*
in the parameter list means that all subsequent parameters are keyword parameters(In our case it isdir_fd
andfollow_symlinks
), That is a non-location parameter.
os.chown Returns
os.chown
method does not return a value.
os.chown Demo 1: Basic Usage
# Api demos for os.chown() method
import os
path = "apidemos.txt"
# Print the UID and GID of the file
print("UID of the file:", os.stat(path).st_uid)
print("GID of the file:", os.stat(path).st_gid)
os.chown(path, 4000, 4000)
print("\nUID and GID of this file has changed")
# Print the UID and GID of the file again
print("\nOwner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid)
Output:
os.chown Demo 2: Modify UID only
# Api demos for os.chown() method
import os
path = "apidemos.txt"
# Print the UID and GID of the file
print("UID of the file:", os.stat(path).st_uid)
print("GID of the file:", os.stat(path).st_gid)
os.chown(path, 5000, -1)
print("\nUID of this file has changed")
# Print the UID and GID of the file again
print("\nOwner id of the file:", os.stat(path).st_uid)
print("Group id of the file:", os.stat(path).st_gid)
Output:
os.chown Demo 3: Modify the UID and GID of the symbolic link file itself
# Api demos for os.chown() method
import os
path = "apidemos.txt"
# using os.symlink() method to create a symlink for path
path_symlink = "apidemos_symlink.txt"
os.symlink(path, path_symlink)
# Print the UID and GID of the path and path_symlink
print("UID of the path:", os.stat(path).st_uid)
print("GID of the path:", os.stat(path).st_gid)
print("UID of the path_symlink:", os.stat(path_symlink).st_uid)
print("GID of the path_symlink:", os.stat(path_symlink).st_gid)
# Case 1: change the path_symlink file's UID and GID with default follow_symlinks
os.chown(path_symlink, 4000, 4000)
# Print the UID and GID of the path and path_symlink
print("Case 1: change the path_symlink file's UID and GID with default follow_symlinks to 1000")
print("UID of the path:", os.stat(path).st_uid)
print("GID of the path:", os.stat(path).st_gid)
print("UID of the path_symlink:", os.stat(path_symlink).st_uid)
print("GID of the path_symlink:", os.stat(path_symlink).st_gid)
# Case 2: change the path_symlink file's UID and GID with follow_symlinks=False
os.chown(path_symlink, 3000, 3000, follow_symlinks = False)
# Print the UID and GID of the path and path_symlink
print("Case 2: change the path_symlink file's UID and GID with follow_symlinks=False to 2000")
print("UID of the path:", os.stat(path).st_uid)
print("GID of the path:", os.stat(path).st_gid)
print("UID of the path_symlink:", os.stat(path_symlink).st_uid)
print("GID of the path_symlink:", os.stat(path_symlink).st_gid)
Output:
We can see that when follow_symlinks = False
is set, the execution of os.chown
only modifies the UID and GID of the symlink file itself, the UID and GID of the linked file remain unchanged.