How to Get the Current Folder Name in Python

I was working on a file management project, and I needed to quickly grab the current folder name where my Python script was running.

At first, I thought it would be a one-line solution, but then I realized there are multiple ways to do this, depending on whether you want the full path or just the folder name.

In this tutorial, I’ll walk you through the different methods I use in real-world projects to get the current folder name in Python. I’ll use both the os module and the modern pathlib module, so you can pick whichever feels more natural.

Method 1 – Use the os Module

The os module is one of the oldest and most reliable ways to interact with your operating system in Python.

Here’s how I use it to get the current folder name:

import os

# Get the current working directory
current_directory = os.getcwd()

# Extract the folder name from the path
folder_name = os.path.basename(current_directory)

print("Current Directory Path:", current_directory)
print("Current Folder Name:", folder_name)

Output Example (on Windows)

Current Directory Path: C:\Users\Path
Current Folder Name: code

I executed the above example code and added the screenshot below.

python get current folder name

This method is simple and works across Windows, macOS, and Linux. I often use it when I’m writing scripts that need to log the current folder name into a report or save outputs in a structured directory.

Method 2 – Use pathlib.Path

Over the years, I’ve started preferring the pathlib module because it makes working with paths more effective and readable.

Here’s how I do the same thing with pathlib:

from pathlib import Path

# Get the current working directory as a Path object
current_path = Path.cwd()

# Extract the folder name
folder_name = current_path.name

print("Current Directory Path:", current_path)
print("Current Folder Name:", folder_name)

Output Example (on macOS)

Current Directory Path: /Users/Path
Current Folder Name: code

I executed the above example code and added the screenshot below.

python get current directory

Notice how clean and intuitive this looks compared to os. If you’re starting fresh, I recommend using pathlib in your projects.

Method 3 – Get the Folder of the Current File

Sometimes, the script file may not run from the same folder where it’s saved. In that case, using os.getcwd() or Path.cwd() won’t give you the folder name of the script itself.

Here’s how I handle that:

import os

# Get the absolute path of the current file
file_path = os.path.abspath(__file__)

# Get the folder path of the file
folder_path = os.path.dirname(file_path)

# Extract the folder name
folder_name = os.path.basename(folder_path)

print("Current File Path:", file_path)
print("Folder Path:", folder_path)
print("Folder Name:", folder_name)

Output Example (on Linux)

Current File Path: /User/Path
Folder Path: /home/Path
Folder Name: code

I executed the above example code and added the screenshot below.

get current directory python

This method is especially useful if you package your Python scripts into modules or when you’re deploying them in different environments.

Method 4 – Use pathlib for the Current File

Here’s the same approach but with pathlib, which I find much cleaner:

from pathlib import Path

# Get the current file path
file_path = Path(__file__).resolve()

# Get the parent folder
folder_name = file_path.parent.name

print("Current File Path:", file_path)
print("Current Folder Name:", folder_name)

Output Example

Current File Path: C:\Users\Sarah\Desktop\PythonApp\main.py
Current Folder Name: PythonApp

This is my go-to method when working with multi-file projects where I need to reference files relative to the script’s location.

Common Use Cases

Here are a few real-world scenarios where I’ve used these methods:

  1. Organizing Output Files – Automatically saving reports in the same folder as the script.
  2. Logging – Adding the folder name to log files for better traceability.
  3. Dynamic Imports – Loading modules or data files relative to the script’s folder.
  4. Cross-Platform Projects – Ensuring the script runs smoothly on both Windows and Linux servers.

Which Method Should You Use?

  • If you just need the current working directory → Use os.getcwd() or Path.cwd().
  • If you need the folder of the script file → Use __file__ with os.path or pathlib.
  • If you’re starting a new project → I recommend pathlib because it’s modern, clean, and powerful.

Most of the time, I use pathlib because it feels more natural and avoids the extra function calls you see with os.path. But if you’re working with older Python codebases, you’ll likely run into os more often.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.