How to change the owner of a directory using Python?


By utilising the pwd, grp, and os modules, you can modify the owner of a file or directory. To obtain the user ID from the user name, to obtain the group ID from the group name string, and to modify the owner, one uses the uid module.

Following are the methods to change the owner of a directory using Python −

Using os.chown() Method

To change the owner and group id of the given path to the specified numeric owner id (UID) and group id(GID), use Python's os.chown() method.

Syntax

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

Where,

filepath is the file descriptor for the file whose uid and gid are to be modified.

uid is an integer value that represents the path's owner id.

gid is a number that represents the group id that should be entered for the path (set any one of the ids to -1 to keep it unchanged), a file descriptor relating to a directory is called dir_fd (it is optional) and its parameter's default value is None, if follow_symlinks is selected, its parameter's default value is True. We can set it to False if we do not want the os.chown() technique to follow symlinks. If it returns False, the procedure will work on the symbolic link rather than the file it links to.

Note − The symbol "*" in the parameter list denotes that all subsequent parameters (in this case, "dir fd" and "follow symlinks") are keyword-only parameters and can only be supplied by name, not as a positional parameter.

No value is returned by this method.

Example - 1(LINUX)

Following is an example to change the owner of a directory using os.chown() method −

import os path = "C:\Users\Lenovo\Downloads\Work TP\trial.py" print("file's owner id:", os.stat(path).st_uid) print("file's group id:", os.stat(path).st_gid) # Changing the owner id and group id of the file uid = 1500 gid = 1500 os.chown(path, uid, gid) print("\nOwner id and group id of the file is changed succesfully") # Printing the owner id and group id of the file print("\nfile's owner id now is:", os.stat(path).st_uid) print("file's group id now is:", os.stat(path).st_gid)

Output

Following is an output of the above code −

└─$ sudo python3 sarika.py
file's owner id: 100
file's group id: 100
Owner id and group id of the file is changed successfully
file's owner id now is: 1500
file's group id now is: 1500

Example - 2 (Linux)

Following is an example while setting one id and leaving the other unchanged −

import os # File path = "C:\Users\Lenovo\Downloads\Work TP\trial.py" # Printing the current owner id and group id print("file's owner id:", os.stat(path).st_uid) print("file's group id:", os.stat(path).st_gid) # Changing only the owner id and leaving the group id unchanged # setting id as -1 to leave the group id unchanged uid = 200 gid = -1 os.chown(path, uid, gid) print("\nOwner id of the file is changed successfully leaving the group id unchanged") # Printing the owner id and group id of the file now print("\nfile's owner id now is:", os.stat(path).st_uid) print("file's group id now is:", os.stat(path).st_gid)

Output

Following is an output of the above code.

└─$ sudo python3 sarika.py
[sudo] password for sarika:
file's owner id: 1500
file's group id: 1000

Owner id of the file is changed successfully leaving the group id unchanged

file's owner id now is: 200
file's group id now is: 1000

Example - 3 (Linux)

Following is an example if the given path is a symlink −

import os filepath = "C:\Users\Lenovo\Downloads\Work TP\trial.py" # Create a symlink symlink = "C:\Users\Lenovo\Downloads\Work TP\trial(symlink).py" os.symlink(filepath, symlink) print("file's owner id:", os.stat(filepath).st_uid) print("file's group id:", os.stat(filepath).st_gid) print("symlink's owner id:", os.stat(symlink).st_uid) print("symlink's group id:", os.stat(symlink).st_gid) # Changing the owner of the symlink uid = 200 gid = 200 os.chown(symlink, uid, gid) print("\nfile's owner id and group id is changed successfully") # Printing the owner id, the group id and the symlink print("\nfile's owner id:", os.stat(filepath).st_uid) print("file's group id:", os.stat(filepath).st_gid) print("symlink's owner id:", os.stat(symlink).st_uid) print("symlink's group id:", os.stat(symlink).st_gid) # Changing the owner of symlink pointing uid = 400 gid = 400 os.chown(symlink, uid, gid, follow_symlinks = False) print("\n The owner id and group id did not change") # Printing the owner id, the group id and the symlink print("\nfile's owner id:", os.stat(filepath).st_uid) print("file's group id:", os.stat(filepath).st_gid) print("symlink's owner id:", os.stat(symlink).st_uid) print("symlink's group id:", os.stat(symlink).st_gid)

Output

Following is an output of the above code −

└─$ sudo python3 code1.py 
[sudo] password for govind: 
file's owner id: 1000 file's group id: 1000 
symlink's owner id: 1000 
symlink's group id: 1000 

file's owner id and group id is changed successfully 

file's owner id: 200 
file's group id: 200 
symlink's owner id: 200 
symlink's group id: 200

The owner id and group id did not change
   
file's owner id: 200 
file's group id: 200 
symlink's owner id: 200 
symlink's group id: 200

Using shutil.chown() Method

The Python Shutil module offers a wide range of high-level actions on files and file collections. It falls under one of Python's common utility modules. This module helps in automating the processes of changing a file's ownership and deleting a directory.

You can modify the owner and/or group of the given path in Python using the shutil.chown() method.

Syntax

shutil.chown(filepath, user = None, group = None)

Example

Following is an example to change owner of a file using shutil.chown() method −

import shutil from pathlib import Path path = 'C:\Users\Lenovo\Downloads\Work TP\trial.py'
# Getting the owner and the user info = Path(path) user = info.owner() group = info.group() print("Present owner and group") print("Present owner:", user) print("Present group:", group) #changing the owner and the group uid = 10 gid = 10 shutil.chown(path, uid, gid) print("\nThe owner and the group is changed successfully") # Printing the owner user and group info = Path(path) user = info.owner() group = info.group() print("Present owner now:", user) print("Present group now:", group)

Output

Following is an output of the above code −

$ sudo python3 code2.py
[sudo] password for sarika:
Present owner and group
Present owner: sarika
Present group: sarika

The owner and the group is changed successfully
Present owner now: uucp
Present group now: uucp

Updated on: 18-Aug-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements