Python os.fchdir() Method
os.fchdir() Overview
The os.fchdir() method changes the current working directory by file descriptor.
Available on Unix.
os.fchdir() Syntax
The syntax format of the fchdir() method is as follows.
os.fchdir(fd);
os.fchdir() Parameters
- fd – file descriptor
os.fchdir() Return Value
This method has no return value.
os.fchdir() Demo
The following example demonstrates the use of the fchdir() method.
#!/usr/bin/python3
import os, sys
# First go to the directory "/var/www/html"
os.chdir("/var/www/html" )
# Output current directory
print ("The current working directory is : %s" % os.getcwd())
# Open the new directory "/tmp"
fd = os.open( "/tmp", os.O_RDONLY )
# Use the os.fchdir() method to modify to the new directory
os.fchdir(fd)
# Output current directory
print ("The current working directory is : %s" % os.getcwd())
# Close the open directory
os.close( fd )
The output of executing the above program is:
The current working directory is : /var/www/html
The current working directory is : /tmp