Python | os.chroot () method

Python Methods and Functions

os.chroot() in Python is used to change the root of the current process to path.

Syntax: os.chroot (path)

Parameters:
path: path to be set as root for the current process.

Returns: does not return any value.

Code # 1:

# Python program to explain os.chroot () ethod

 

import os, sys

  
# Set current root path to / Geeks / gfg

os.chroot ( "/ Geeks / gfg" )

pri nt ( "root path successfully changed." )

Output:

 root path successfully changed. 

Code # 2:

# Function to change the root directory of the process.

def change_root_directory (path):

 

try :

os.chdir (path)

os.chroot (path)

  except Exception as exc:

error = DaemonOSEnvironmentError ( "Unable to change root directory ({exc})" . format (exc = exc))

raise error 

 
# main function

change_root_directory ( "/ Geeks / gfg" )