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.chdir() method in Python used to change the current working directory to specified path. It takes only a single argument as new directory path.
path: A complete path of directory to be changed to new directory path.
Returns: Doesn’t return any value
Code #1: Use chdir() to change the directory
Code #2: Use of os.getcwd()
To know the current working directory of the file, getcwd() method can be used. After changing the path, one can verify the path of current working directory using this method.
Current working directory is: c:\\gfg_dir
Code #3: Handling the errors while changing the directory
Inserting inside- c:\gfg_dir\gfg Something wrong with specified directory. Exception- Restoring the path Current directory is- c:\gfg_dir\gfg
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.