Python Get Current Folder Name

This Python tutorial will teach you about Python Get Current Folder Name. You will use the two modules in Python to get the current folder name.

I have saved the image in a directory or folder for image processing. To access the photos stored on my disk for the project, I used the function from the ‘os’ to get the directory’s path containing images.

So, in this tutorial, I have explained how to use the basename() function to get the folder name. I also explained a module called ‘pathlib’, which helps retrieve the folder name.

After completing this tutorial, you will be able to retrieve the folder name from the operating system and learn how to get the absolute path of the current working directory.

Python Get Current Folder Name

There are two Python modules that you can use to get the name of the current folder or working directory.

First is the ‘os’ module, and second is the ‘pathlib’ module. Let’s start.

Python Get Current Folder Name Using OS Module

The ‘os’ module in Python allows you to interact with operating systems. It has several functions that can be used to interact with operating system file systems.

One of the functions is getcwd(), which returns the absolute path of the current folder. The absolute path means the complete and direct address of a specific file or folder location.

For example, the getcwd() returns the absolute path like this on Windows ‘c:\users\data’.

READ:  Extend vs Append in Python | Difference between Append and Extend in Python [6 Major Key Differences]

But this function returns the path of the file or folder; we need the folder name, and for that, you need to use one more method called basename() from the submodule ‘os.path’.

This function basename () takes the path to a file or folder and extracts the file’s name or the last folder from that path. For example, passing the absolute path ‘c:\users\data’ to the basename() function returns the file or folder name like ‘data’.

Simply put, the basename() function takes the path and returns the tail part after the last slash ‘/’ on Unix/Linux/MacOS or ‘\’ on Windows.

Let’s see with an example. I am using Windows 11. Look at the image below; suppose you must get the’ Project’ folder name.

python get folder name

To get the folder name in the image, open your current directory in the command prompt. Then, type Python in your prompt to open the Python shell using the code below.

python

After entering the Python shell, import the ‘os’ module using the code below.

import os

Then, get the absolute path of the current directory or folder whose name you want to get. For that, use the getcwd() function, as shown below.

path = os.getcwd()

After that, to extract the folder name from the ‘path’, pass it to the basename() function, as shown below.

currentFolderName = os.path.basename(path)
print(currentFolderName)
Python Get Current Folder Name Using OS Module

Look at the output; the basename() function returns the folder name ‘Project’ in the prompt.

This is how to get the current folder name using the basename() function of submodule ‘os.path‘.

Python Get Current Directory Name Using Pathlib Module

The module ‘pathlib’ is a new module alternative to ‘os’, introduced in Python 3.4; it uses object-oriented concepts to file path systems.

READ:  ModuleNotFoundError: No module named Django

To get the current directory name using the ‘pathlib’, you must use this module’s class ‘Path’. This class represents the file system path and provides different functions to operate on these paths, such as reading or writing content.

Let’s see how to open the command prompt and type Python to open the shell.

from pathlib import Path

The next step is to get the path of the current working folder or directory. For that class, ‘Path’ has a function called cwd(), which returns the absolute path of the current working directory. Execute the code below in your prompt.

path= Path.cwd()
print(path)

The above code returns the path like ‘C:\Users\saura\OneDrive\Desktop\Project’ in my case.

To extract the folder name from that path, you must call the ‘name’ attribute on the ‘path’ as shown below.

currentFolderName = path.name
print(currentFolderName)
Python Get Current Directory Name Using Pathlib Module

Look at the output; the code ‘path.name’ returns the folder name as ‘Project’. If you want to get the name of another folder, change the current working folder or directory to that folder, then follow the above steps again. You will get the folder name.

From the above examples, I hope you understand how to get the folder name in Python using the ‘os’ and ‘pathlib’ modules.

Conclusion

In this Python tutorial, you learned python get current folder name using the basename() function in the ‘os’ and attribute ‘name’ in the ‘pathlib’ modules.

You may like to read: