Python os.fchdir() Method



The Python os.fchdir() method changes the current working directory to the directory represented by the file descriptor. The descriptor must refer to an opened directory, not an open file.

The directory in which all commands are being performed is known as the current working directory. It can be a database file, a folder, a library, or a directory. The current working directory is also known as the present working directory or just the working directory. This method does not return any value.

Note: This method is only available in UNIX/LINUX platforms.

Syntax

Following is the syntax of Python os.fchdir() method −

os.fchdir(fd)

Parameters

  • fd − This is a directory descriptor.

Return Value

This method does not return any value.

Example

The following example shows the usage of Python os.fchdir() method. Here, the current working is changed to the directory "/tmp" represented by the file descriptor 'fd'. The getcwd() method is used to know the current working directory of the file.

import os, sys
# First go to the "/var/www/html" directory
os.chdir("/var/www/html" )
# Print current working directory
print ("Current working dir : %s" % os.getcwd())
# Now open a directory "/tmp"
fd = os.open( "/tmp", os.O_RDONLY )
# Use os.fchdir() method to change the dir
os.fchdir(fd)
# Print current working directory
print ("Current working dir : %s" % os.getcwd())
# Close opened directory.
os.close( fd )

When we run above program, it produces following result −

Current working dir : /var/www/html
Current working dir : /tmp

Example

If the file descriptor represents an open file instead of an opened directory, then this method raises 'NotADirectoryError' exception.

import os
path = "code.txt"
# opening the above path  
# getting the file descriptor associated with it
filedesc = os.open(path, os.O_RDONLY)
# Changing the working directory 
os.fchdir(filedesc)
print("Current working directory is changed succesfully")
# Printing the working directory 
print("The current working directory is:", os.getcwd())

While executing the above code we get the following output −

Traceback (most recent call last):
 File "/home/sarika/Desktop/chown.py", line 7, in <module>
   os.fchdir(filedesc)
NotADirectoryError: [Errno 20] Not a directory

Example

In here, we are trying to handle the possible errors using os.fchdir() method.

import os
filepath = "code.txt"
# opening the above path  
# getting the file descriptor associated with it
try :
   filedesc = os.open(filepath, os.O_RDONLY)
   # Changing the working directory
   try :
      os.fchdir(filedesc)
      print("Current working directory is changed successfully")
      # Printing the working directory 
      print("The current working directory is:", os.getcwd())

   # If file descriptor does not represents a directory
   except NotADirectoryError:
      print("The provided file descriptor does not represent an opened directory")
# If the file path does not exists
except FileNotFoundError:
   print("The file path does not exists")
# permission related issue while opening the provided path
except PermissionError:
   print("Permission is denied")

Following is an output of the above code −

The provided file descriptor does not represent an opened directory
python_file_methods.htm
Advertisements