Latest web development tutorials

Python3 os.fchdir () method

Python3 OS file / directory methods Python3 OS file / directory methods


Outline

os.fchdir () method of file descriptors to change the current working directory.

Unix, is available on Windows.

grammar

fchdir () method syntax is as follows:

os.fchdir(fd);

parameter

  • fd - file descriptor

return value

This method has no return value.

Examples

The following example demonstrates fchdir () method of use:

#!/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 above program output is:

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

Python3 OS file / directory methods Python3 OS file / directory methods