📜  Python – os.chroot() 方法

📅  最后修改于: 2022-05-13 01:54:33.836000             🧑  作者: Mango

Python – os.chroot() 方法

Python中的os.chroot()方法用于将当前进程的根目录更改为路径。

代码#1:

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


Python3
# 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")


输出:

root path successfully changed.


代码#2:

Python3

# 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")