OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
os.fchdir() method in Python is used to change the current working directory to the directory represented by the given file descriptor.
A file descriptor is small integer value that corresponds to a file or other input/output resource, such as a pipe or network socket. A File descriptor is an abstract indicator of a resource and act as handle to perform various lower level I/O operations like read, write, send etc.
For Example: Standard input is usually file descriptor with value 0, standard output is usually file descriptor with value 1 and standard error is usually file descriptor with value 2.
Further files opened by the current process will get the value 3, 4, 5 an so on.
os.fchdir() method is equivalent to os.chdir(file_descriptor) method.
Syntax: os.fchdir(fd)
Parameter:
fd: A file descriptor. The file descriptor must represent an opened directory not an open file.Return Type: This method does not return any value.
# Python program to explain os.fchdir() method # importing os module import os # Print the current working # directory using os.getcwd() method print("Current working directory:", os.getcwd()) # Path path = "/home/ihritik/Documents" # open the directory represented by # the above given path and get # the file descriptor associated # with it using os.open() method fd = os.open(path, os.O_RDONLY) # Change the current working # directory using os.fchdir() method os.fchdir(fd) print("Current working directory changed") # Print the current working # directory using os.getcwd() method print("Current working directory:", os.getcwd()) |
Current working directory: /home/ihritik Current working directory changed Current working directory: /home/ihritik/Documents
# Python program to explain os.fchdir() method # importing os module import os # Path path = "/home/ihritik/Documents/file.txt" # open the above path and get # the file descriptor associated # with it using os.open() method fd = os.open(path, os.O_RDONLY) # The file descriptor must # represent an open file # instead of an opened directory # The method will raise # 'NotADirectoryError' exception # Change the current working # directory using os.fchdir() method os.fchdir(fd) print("Current working directory changed") # Print the current working # directory using os.getcwd() method print("Current working directory:", os.getcwd()) |
Traceback (most recent call last):
File "changeDir.py", line 24, in
os.fchdir(fd)
NotADirectoryError: [Errno 20] Not a directory
# Python program to explain os.fchdir() method # importing os module import os # Path path = "/home/ihritik/Desktop/file.txt" # try opening the above path and get # the file descriptor associated # with it using os.open() method try : fd = os.open(path, os.O_RDONLY) # Try Changing the current working # directory using os.fchdir() method try : os.fchdir(fd) print("Current working directory changed") # Print the current working # directory using os.getcwd() method print("Current working directory:", os.getcwd()) # Catch exceptions # If file descriptor does # not represents a directory except NotADirectoryError: print("The given file descriptor does \ not represent a directory") # Catch exceptions # If path does not exists except FileNotFoundError: print("Path does not exists") # If there is any permission # related issue while opening # the given path except PermissionError: print("Permission denied") |
The given file descriptor does not represent a directory
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- class method vs static method in Python
- Difference between Method Overloading and Method Overriding in Python
- Get() method for dictionaries in Python
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- Python Program to detect the edges of an image using OpenCV | Sobel edge detection method
- Line detection in python with OpenCV | Houghline method
- Python | os._exit() method
- Python | os.WEXITSTATUS() method
- Python | os.abort() method
- Python | os.renames() method
- Python | os.lseek() method
- Python calendar module | formatmonth() method
- Python | PyTorch sin() method
- Python | Sympy Line.is_parallel() method
- Python PIL | GaussianBlur() method
- Python | range() method
- Python | Numpy np.hermefit() method
- Python | Numpy np.hermevander() method
- Python String Title method
- Python groupby method to remove all consecutive duplicates
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

