Python | os.chroot() method

os.chroot() method in Python is used to change the root directory 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.chroot() ethod
  
import os, sys
  
# Set current root path to /Geeks/gfg
os.chroot("/Geeks/gfg")
print ("root path successfully changed.")

chevron_right


Output:

root path successfully changed.

 
Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Function to Change 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")

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.