Python Practice › OS Module Programs › Program 41
Change root with os.chroot()
Write a Python program that uses os.chroot() where available.
Output
os.chroot is not available on this platform
Approach
chrootis Unix-only and needs root privileges, so guard it.
Solution
import os if not hasattr(os, "chroot"): print("os.chroot is not available on this platform")elif os.geteuid() != 0: print("os.chroot needs root privileges")else: os.chroot("/tmp") print("root changed")Output
os.chroot is not available on this platform
Note
chroot limits which part of the file system a process can see. It is used by system tools, rarely in everyday scripts.
Learn the concept
New to this topic? Read the lesson, then come back.