In this Python tutorial, I will explain to you, how to get current directory name in Python. By using the Python os module, we will see, how to get current folder name in Python.
Python os module
Before we start, it is essential to understand the ‘os’ module. Python’s os
module provides functions to interact with the operating system. This module comes in handy when we want to work with files and directories. One of the common tasks we can do with this module is to get the current directory name.
Get Current Directory in Python Using os.getcwd()
The getcwd()
function of the os module returns the current working directory in Python. Here’s how we can use it to get the current folder in Python.
import os
current_directory = os.getcwd()
print(current_directory)
When you run this code, it will print the full path of your current working directory.
To get just the name of the current directory (and not the full path) or the folder name, we can use the os.path.basename()
function. Here’s how we can use it:
import os
current_directory = os.getcwd()
directory_name = os.path.basename(current_directory)
print(directory_name)
This code will print the name of the current directory in Python, without the full path. Or this is how to get the folder name in Python.
Get Directory Name Using os.path.dirname()
Another function from the os
module that we can use to get the directory name is os.path.dirname()
. The dirname()
function returns the directory component of a pathname. However, in most cases, if we want to get the current directory, we still have to use it in conjunction with os.getcwd()
:
import os
current_directory = os.getcwd()
print(os.path.dirname(current_directory))
This code will print the directory that contains the current directory, not the current directory itself.
To get just the name of the current directory (and not the full path), we can use the os.path.basename()
function in conjunction with os.path.dirname()
:
import os
current_directory = os.getcwd()
directory_name = os.path.basename(os.path.dirname(current_directory))
print(directory_name)
get the current directory name in Python using pathlib
Pathlib is a module in Python used for object-oriented filesystem paths. It was designed to be simple to use and to represent filesystem paths with semantics appropriate for different operating systems. Here’s how to get the current directory using pathlib in Python:
from pathlib import Path
# Get current directory
current_directory = Path.cwd()
print(current_directory)
Just like with the os
module, this will print the full path of the current directory. To get just the name of the current directory, we can use the .name
attribute:
from pathlib import Path
# Get current directory
current_directory = Path.cwd()
# Get current directory name
directory_name = current_directory.name
print(directory_name)
Conclusion
In this Python tutorial, We’ve looked at different methods to get the current directory in Python, using both the os
and pathlib
modules.
Remember, os.getcwd()
and Path.cwd()
give you the current working directory as a full path in Python. If you want only the name of the current directory, you can use os.path.basename(os.getcwd())
or Path.cwd().name
.
You may also like:
- Read a file line by line in Python
- Python Program to Write List to File
- How to read a CSV file in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.