Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python os.fchdir() method

Python os.fchdir() method

Python File methods Python OS file/directory methods


Overview

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

Available on Unix and Windows.

Syntax

The **fchdir()** method syntax is as follows:



os.fchdir(fd);

Parameters

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



# 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

Python File methods Python OS file/directory methods