Python3 os.fchdir() Method
Python3 OS File/Directory Methods
Overview
The os.fchdir() method changes the current working directory through a file descriptor.
Available on Unix.
Syntax
The **fchdir()** method syntax is as follows:
os.fchdir(fd);
Parameters
-
fd -- The file descriptor
Return Value
This method has no return value.
Example
The following example demonstrates the use of the fchdir() method:
#!/usr/bin/python3
import os, sys
# First go to directory "/var/www/html"
os.chdir("/var/www/html" )
# Print current directory
print ("当前工作目录为 : %s" % os.getcwd())
# Open new directory "/tmp"
fd = os.open( "/tmp", os.O_RDONLY )
# Change to a new directory using os.fchdir() method
os.fchdir(fd)
# Print current directory
print ("当前工作目录为 : %s" % os.getcwd())
# Close the opened directory
os.close( fd )
The output of the above program is:
当前工作目录为 : /var/www/html
当前工作目录为 : /tmp