YouTip LogoYouTip

Os Fchdir

# Python2.x Python os.fchdir() Method [![Image 3: Python File Methods](#) Python OS File/Directory Methods](#) * * * ### Overview The os.fchdir() method changes the current working directory via a file descriptor. Available on Unix, Windows. ### Syntax The **fchdir()** method syntax format 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 # First go to directory "/var/www/html" os.chdir("/var/www/html" )# Print current directoryprint "Current working directory: %s" % os.getcwd()# Open new directory "/tmp" fd = os.open( "/tmp", os.O_RDONLY )# Use os.fchdir() method to change to new directory os.fchdir(fd)# Print current directoryprint "Current working directory: %s" % os.getcwd()# Close opened directory os.close( fd ) The output of the above program execution is: Current working directory: /var/www/html Current working directory: /tmp [![Image 4: Python File Methods](#) Python OS File/Directory Methods](#)
← Os FdopenOs Closerange β†’