Python | shutil.chown() method

12 Oct, 2021
Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating process of chowning and removal of files and directories.shutil.chown() method in Python is used to change the owner and /or group of the specified path.  
Syntax: shutil.chown(path, user = None, group = None)Parameters: path: A string value representing a valid path. user: A string value representing a system user group: A string value representing a group user and group can be also given by user id (uid) and group id (gid) respectively.Return Type: This method does not return any value.  
Code #1: Use of shutil.chown() method to change owner and group of the specified path  
Python3
# Python program to explain shutil.chown() method   # importing shutil moduleimport shutil # importing Path class of pathlib modulefrom pathlib import Path  # Pathpath = '/home/ihritik/Desktop/file.txt'  # Get the owner and group# of the specified path# using Path.owner() and# Path.group() methodinfo = Path(path)user = info.owner()group = info.group() # Print owner and group# of the specified pathprint("Current owner and group of the specified path")print("Owner:", user)print("Group:", group)  # Now, change the owner and group# of the specified pathuser = 'ihritik'group = 'ihritik'shutil.chown(path, user, group) print("\nOwner and group changed")  # Print the owner and group# of the specified pathinfo = Path(path)user = info.owner()group = info.group()print("Current owner:", user)print("Current group:", group)  # Change only group# of the specified path# and let owner as it isgroup = 'root' shutil.chown(path, group = group) print("\nOnly group changed") # Print the owner and# group of the specified pathinfo = Path(path)user = info.owner()group = info.group()print("Current owner:", user)print("Current group:", group)  # Similarly, we can change# only owner of the# specified path and let# group as it is
Current owner and group of the specified path
Owner: root
Group: root

Owner and group changed
Current owner: ihritik
Current group: ihritik

Only group changed
Current owner: ihritik
Current group: root
 
Code #2: Use of shutil.chown() method  
Python3
# Python program to explain shutil.chown() method   # We can also change owner# and group of the specified path# by passing owner id (uid) and# group id (gid) as parameter# instead of passing name of# owner and / or group  # importing shutil moduleimport shutil # importing Path class of pathlib modulefrom pathlib import Path  # Pathpath = '/home/ihritik/Desktop/file.txt'  # Get the owner user and# group of the specified path# using Path.owner() and# Path.group() methodinfo = Path(path)user = info.owner()group = info.group()print("Current owner and group of the specified path")print("Current owner:", user)print("Current group:", group)  # Now, change the owner user# and group of the# specified path uid = 0gid = 0shutil.chown(path, uid, gid) print("\nOwner and group changed")  # Print the owner user and# group of the specified pathinfo = Path(path)user = info.owner()group = info.group()print("Current owner:", user)print("Current group:", group)
Current owner and group of the specified path
Owner: ihritik
Group: ihritik

Owner and group changed
Current owner: root
Current group: root
 
Reference: https://docs.python.org/3/library/shutil.html 
gabaa406
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Iterate over a list in Python
How to iterate through Excel rows in Python?
Enumerate() in Python
Python Dictionary
Python OOPs Concepts
Different ways to create Pandas Dataframe
*args and **kwargs in Python
Python Classes and Objects
Introduction To PYTHON
Stack in Python