Python3 os.fchdir() Method

Python3 OS 文件/目录方法Python3 OS File/Directory Methods


Overview

The os.fchdir() method changes the current working directory through a file descriptor.

Available on Unix.

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/python3

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 the above program is:

当前工作目录为 : /var/www/html
当前工作目录为 : /tmp

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions