
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Change Current Directory in Python Shell
A portable method of interacting with the operating system is offered through the OS Python Module. This module is a part of the default Python library, which contains tools for locating and modifying the working directory.
When we are working in the Python shell or scripting environment, it's important to know which directory we're currently working in. This is known as the current working directory. We can change the current working directory to navigate our file system programmatically. In this article, we will see how to change the current working directory in the Python shell.
Getting the current working directory - os.getcwd()
The function os.getcwd() in Python can be used to get the current working directory, and this function returns a string str which is the absolute path to Python's current working directory.
Example
Here in this example, we are using the function os.getcwd() to get the current working directory -
# Importing the module import os # Getting the current working directory cwd = os.getcwd() # Printing the current working directory print("Th Current working directory is: {0}".format(cwd)) # Printing the type of the returned object print("os.getcwd() returns an object of type: {0}".format(type(cwd)))
Following is an output of the above program -
The Current working directory is: C:\Users\Lenovo\Desktopos.getcwd() returns an object of type: ?class 'str'> os.getcwd() returns an object of type:
Change the current working directory: os.chdir()
In Python, we can change the current working directory using the os.chdir() function available in the os module. This is useful when we need to navigate our file system to work with files located in different directories.
The os.chdir() function takes the path as the input argument to change the current working directory to the specified path. After calling this function, all relative file paths will be based on the new current working directory.
Example
Following is an example which shows how to change the current working directory using the function os.chdir() -
# Importing the module import os # Printing the current working directory print("The Current working directory is: {0}".format(os.getcwd())) # Changing the current working directory os.chdir('C:\Users\Lenovo\Downloads\Works') # Print the current working directory print("The Current working directory now is: {0}".format(os.getcwd()))
Here is the output of the above code -
The Current working directory is: C:\Users\Lenovo\Desktop The Current working directory now is: C:\Users\Lenovo\Downloads\Works
Note ? A NotADirectoryError exception will be raised if a directory is not provided as an argument to the chdir() function. There is a FileNotFoundError exception will be raised if the provided directory is not found. A PermissionError exception will be raised if the user running the script doesn't have the required permissions.
Example
In this example, we are implementing the try and exception cases based on the errors may raise while implementing the code using the function chdir() -
# Import the os module import os path = 'C:\Users\Lenovo\Downloads\Works' try: os.chdir(path) print("The Current working directory is: {0}".format(os.getcwd())) except FileNotFoundError: print("Directory: {0} does not exist".format(path)) except NotADirectoryError: print("{0} is not a directory".format(path)) except PermissionError: print("No permissions to change to {0}".format(path))
Output
Following is an output of the above example -
The Current working directory is: C:\Users\Lenovo\Downloads\Works