Python os.fchdir() Method
Python OS File/Directory Methods
Overview
The os.fchdir() method changes the current working directory via a file descriptor.
Available on Unix, Windows.
Syntax
fchdir()The method syntax is as follows:
os.fchdir(fd);
Parameters
fd-- file descriptor
Return Value
This method has no return value.
Example
The following example demonstrates the use of the fchdir() method:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys
# 首先到目录 "/var/www/html"
os.chdir("/var/www/html" )
# 输出当前目录
print "当前工作目录为 : %s" % os.getcwd()
# 打开新目录 "/tmp"
fd = os.open( "/tmp", os.O_RDONLY )
# 使用 os.fchdir() 方法修改到新目录
os.fchdir(fd)
# 输出当前目录
print "当前工作目录为 : %s" % os.getcwd()
# 关闭打开的目录
os.close( fd )
The output of executing the above program is:
当前工作目录为 : /var/www/html 当前工作目录为 : /tmpOther Extensions