Get current directory Python

By: On:

In this python tutorial, you will learn how to get current directory in python.

Get current directory Python

In python, to get the current directory in python we will use os module which has a method getcwd() which will return the current working directory with full path.

The current directory is the folder from where the script is running.

For getting the name of the directory we can use another function called basename from os.path.


import os
directory_path = os.getcwd()
print("My current directory is : " + directory_path)
folder_name = os.path.basename(directory_path)
print("My directory name is : " + folder_name)

After writing the above code (get current directory Python), Ones you will print “directory_path” then the output will appear as “My current directory is E:\project-python“. Here, we used getcwd() for getting the current working directory which is the full path, and basename is used for getting directory name.

You can refer to the below screenshot get current directory Python

Get current directory Python
get current directory Python

You may like following Python tutorials:

In this tutorial, we learned how to get current directory and the name of the directory in Python.

Leave a Comment