How to know/change current directory in Python shell?


A portable method of interacting with the operating system is offered through the OS Python Module. The module, which is a part of the default Python library, contains tools for locating and modifying the working directory.

The following contents are described in this article.

  • How to obtain the current working directory: os.getcwd()
  • Changing the current working directory: os.chdir()

The __file__ function returns the path to the current script file (.py).

Getting the current working directory- os.getcwd()

The function os.getcwd() returns as a string str the absolute path to Python's current working directory. "Get current working directory" (getcwd) refers to the ability to print the current working directory with an operating system using print() and getcwd().

The trailing slash character is omitted from the string that is returned.

Example

Following is an example 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)))

Output

Following is an output of the above code:

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()

Use the chdir() function in Python to change the current working directory.

The path to the directory you wish to change to is the only parameter the method allows. You can use either an absolute or relative path argument.

Example

Following is an example to change the current working directory:

# 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()))

Output

Following is an 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 is thrown if a directory is not provided as an argument to the chdir() method. There is a FileNotFoundError exception raised if the provided directory is not found. A PermissionError exception is raised if the user running the script doesn't have the required permissions.

Example

# 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

Updated on: 26-Aug-2023

36K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements