Easy Tutorial
❮ Python Errors Execptions Python Topological Sorting ❯

Python3 os.fchdir() Method

Python3 OS File/Directory Methods


Overview

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

Available on Unix.

Syntax

The syntax for the fchdir() method is as follows:

os.fchdir(fd);

Parameters

-

fd -- File descriptor

Return Value

This method does not return any value.

Example

The following example demonstrates the use of the fchdir() method:

#!/usr/bin/python3

import os, sys

# First, change to the directory "/var/www/html"
os.chdir("/var/www/html")

# Print the current directory
print("Current working directory is: %s" % os.getcwd())

# Open a new directory "/tmp"
fd = os.open("/tmp", os.O_RDONLY)

# Change to the new directory using os.fchdir()
os.fchdir(fd)

# Print the current directory
print("Current working directory is: %s" % os.getcwd())

# Close the opened directory
os.close(fd)

Executing the above program outputs:

Current working directory is: /var/www/html
Current working directory is: /tmp

Python3 OS File/Directory Methods

❮ Python Errors Execptions Python Topological Sorting ❯