Python | os.chown() method

11 Oct, 2021
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
All functions in os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system.
os.chown() method in Python is used to change the owner and group id of the specified path to the specified numeric owner id (UID) and group id (GID).
Note: os.chown() method is available only on UNIX platforms and the functionality of this method is typically available only to the superuser or a privileged user.
Syntax: os.chown(path, uid, gid, *, dir_fd = None, follow_symlinks = True)
Parameters:path: A file descriptor representing the file whose uid and gid is to be setuid: An integer value representing the owner id to be set for the path.gid: An integer value representing the group id to be set for the path. To leave any one of the ids unchanged, set it to -1.dir_fd (optional): A file descriptor referring to a directory. The default value of this parameter is None.follow_symlinks (optional): The default value of this parameter is True. If we do not want os.chown() method to follow symlink, we can set it to False. If it is False, method will operate on the symbolic link itself instead of the file the link points to.
Note: The ‘*’ in parameter list indicates that all following parameters (Here in our case ‘dir_fd’ and ‘follow_symlinks’) are keyword-only parameters and they can be provided using their name, not as a positional parameter.
Return Type: This method does not return any value.
# Python program to explain os.chown() method     # importing os module import os  # File pathpath = "./file.txt"  # Print the current owner id# and group id of the# specified file path  # os.stat() method will return a # 'stat_result’ object of# ‘os.stat_result’ class whose# 'st_uid' and 'st_gid' attributes# will represent owner id and group id# of the file respectively print("Owner id of the file:", os.stat(path).st_uid)print("Group id of the file:", os.stat(path).st_gid)     # Change the owner id and # the group id of the file# using os.chown() methoduid = 2000gid = 2000os.chown(path, uid, gid)print("\nOwner and group id of the file changed")     # Print the owner id# and group id of the fileprint("\nOwner id of the file:", os.stat(path).st_uid)print("Group id of the file:", os.stat(path).st_gid) 
Output:
# Python program to explain os.chown() method     # importing os module import os  # Filepath = "./file.txt"  # Print the current owner id# and group id of the file  # os.stat() method will return a # 'stat_result’ object of# ‘os.stat_result’ class whose# 'st_uid' and 'st_gid' attributes# will represent owner id and group id# of the file respectively print("Owner id of the file:", os.stat(path).st_uid)print("Group id of the file:", os.stat(path).st_gid)     # Change only owner id of # the file and leave# group id unchanged  # set id as -1 to leave# it unchangeduid = 3000gid = -1os.chown(path, uid, gid)print("\nOwner id of the file changed")    # Print the owner id# and group id of the fileprint("\nOwner id of the file:", os.stat(path).st_uid)print("Group id of the file:", os.stat(path).st_gid) 
Output:
# Python program to explain os.chown() method     # importing os module import os  # File pathpath = "./file.txt"  # Creating a symlink# of the above path  # using os.symlink() methodsymlink = "./file(symlink).txt"os.symlink(path, symlink)    # Print the current owner id# and group id of the file# as well as the symlink pointing# to the above specified file path print("Owner id of the file:", os.stat(path).st_uid)print("Group id of the file:", os.stat(path).st_gid)   print("Owner id of the symlink:", os.stat(symlink).st_uid)print("Group id of the symlink:", os.stat(symlink).st_gid)   # Change the ownership # of the symlink pointing # to the above file './file.txt'uid = 1000gid = 1000os.chown(symlink, uid, gid)print("\nOwner id and group id changed")    # Print the owner id# and group id of the file# as well as the symlinkprint("\nOwner id of the file:", os.stat(path).st_uid)print("Group id of the file:", os.stat(path).st_gid)   print("Owner id of the symlink:", os.stat(symlink).st_uid)print("Group id of the symlink:", os.stat(symlink).st_gid)    # As os.chown() method# follows symlink# so, we can change the# owner and group id # through a symlink     # We can modify the follow_symlinks# parameter if we do not# want os.chown() method to# follow symlink    # Change the ownership # of the symlink pointing # to the above file './file.txt'uid = 4000gid = 4000os.chown(symlink, uid, gid, follow_symlinks = False)print("\nOwner id and group id not changed")    # Print the owner id# and group id of the file# as well as the symlink# pointing to itprint("\nOwner id of the file:", os.stat(path).st_uid)print("Group id of the file:", os.stat(path).st_gid)   print("Owner id of the symlink:", os.stat(symlink).st_uid)print("Group id of the symlink:", os.stat(symlink).st_gid)
Output:
Reference: https://docs.python.org/3/library/os.html
adnanirshad158
python-os-module
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Iterate over a list in Python
Rotate axis tick labels in Seaborn and Matplotlib
Enumerate() in Python
Deque in Python
Stack in Python
Python Dictionary
sum() function in Python
Print lists in Python (5 Different Ways)
Different ways to create Pandas Dataframe
Queue in Python