How to Check if a Directory Exists in Python

To check whether a folder is there, call os.path.isdir(path) or Path(path).is_dir(). Both return True only for an existing folder, never for a file.

In this tutorial, I’ll show you four ways to check if a directory exists in Python, plus how to create the folder when it’s missing.

Every result below comes from real runs on Python 3.12.5 on Windows 11, shown in VS Code.

The sample folder we’ll check

Picture a script that saves monthly sales reports. Its folder has reports/ (with 2026-09 and 2026-10 inside), no archive/ yet, and a file called sales.csv.

The file is the tricky one: it exists, but it isn’t a folder. For the file side of the question, see how to check if a file exists in Python.

Method 1: Check if a directory exists with os.path.isdir() (Recommended)

Use os.path.isdir() when you only need a yes or no. Here’s the code on our sample paths:

import os

paths = ["reports", "reports/2026-10", "archive", "sales.csv"]

for path in paths:
    print(f"{path:<16} isdir: {os.path.isdir(path)}")

Output:

reports          isdir: True
reports/2026-10  isdir: True
archive          isdir: False
sales.csv        isdir: False
VS Code output of os.path.isdir returning True for reports and reports/2026-10 and False for archive and sales.csv
Only the two real folders return True; the missing folder and the CSV file both return False.

How does this code work?

It returns True for the two folders and False for the missing archive and the file sales.csv. It never raises an error. Joining folder names safely is covered in os.path.join in Python.

Method 2: Why os.path.exists() isn’t enough to check a folder

Many answers online use os.path.exists(), but it can’t tell a folder from a file. Same sample folder, both checks side by side:

import os

for path in ["reports", "archive", "sales.csv"]:
    print(f"{path:<10} exists: {os.path.exists(path)!s:<6}"
          f" isdir: {os.path.isdir(path)}")

Output:

reports    exists: True   isdir: True
archive    exists: False  isdir: False
sales.csv  exists: True   isdir: False

sales.csv shows exists: True but isdir: False, so an exists() check would happily save reports into a CSV file. For missing files, see handling a file that does not exist in Python covers the fixes.

Method 3: Check a directory with pathlib Path.is_dir()

pathlib is the modern way, and I use it in new code. Here it is on the same sample folder:

from pathlib import Path

base = Path("reports")
month = base / "2026-10"

print(month, "->", month.is_dir())
print("archive ->", Path("archive").is_dir())
print("sales.csv ->", Path("sales.csv").is_dir())

Output:

reports\2026-10 -> True
archive -> False
sales.csv -> False
VS Code output of pathlib Path.is_dir returning True for reports/2026-10 and False for archive and sales.csv
Path.is_dir() gives the same answers as os.path.isdir().

How does this code work?

base / "2026-10" builds the path, so Windows prints reports\2026-10. is_dir() returns True there and False for archive.

To see what’s inside a folder you found, read listing files in a directory with Python.

Method 4: Skip the check and catch the error with try/except

If the next line opens the folder anyway, skip the check: try it and catch the error. Same sample folder, three paths:

import os

def count_items(folder):
    try:
        names = os.listdir(folder)
    except FileNotFoundError:
        return f"{folder}: missing"
    except NotADirectoryError:
        return f"{folder}: not a folder"
    return f"{folder}: {len(names)} item(s)"

for folder in ["reports", "archive", "sales.csv"]:
    print(count_items(folder))

Output:

reports: 2 item(s)
archive: missing
sales.csv: not a folder
VS Code output of os.listdir in try/except reporting reports with 2 items, archive missing and sales.csv not a folder
One function covers all three cases: a real folder, a missing one, and a file.

How does this code work?

os.listdir() finds 2 items in reports, raises FileNotFoundError for archive and NotADirectoryError for sales.csv. There’s no gap where another program could delete the folder between check and use.

How do you create a directory if it doesn’t exist in Python?

Don’t write if not isdir(): mkdir(); let mkdir() do both jobs. Same sample folder, where archive is missing:

from pathlib import Path

target = Path("archive") / "2026-10"
print("before:", target.is_dir())

target.mkdir(parents=True, exist_ok=True)   # creates both levels
print("after: ", target.is_dir())

target.mkdir(parents=True, exist_ok=True)   # already there: no error
print("again: ", target.is_dir())

try:
    Path("sales.csv").mkdir(exist_ok=True)  # a FILE has that name
except FileExistsError as err:
    print("FileExistsError:", err.strerror)

Output:

before: False
after:  True
again:  True
FileExistsError: Cannot create a file when that file already exists
VS Code output of Path.mkdir with parents=True and exist_ok=True creating archive/2026-10 and FileExistsError for sales.csv
The first call creates the folder, the second does nothing, and a file with the same name still raises FileExistsError.

How does this code work?

parents=True creates both levels, and exist_ok=True makes the second call a no-op.

The catch: exist_ok only forgives a folder, so sales.csv still raises FileExistsError. More in creating a directory in Python.

Why does Python say my directory doesn’t exist when it does?

Usually it’s a relative path. "reports" is looked up from the working directory, not your script’s folder. This script runs from one folder up:

import os
from pathlib import Path

here = Path(__file__).resolve().parent
os.chdir(here.parent)            # launched from one folder up

print("relative 'reports':", os.path.isdir("reports"))
print("script-based path: ", (here / "reports").is_dir())
print("upper-case name:   ", (here / "REPORTS").is_dir())

Output:

relative 'reports': False
script-based path:  True
upper-case name:    True

The relative check fails, the __file__ path works, and REPORTS matches because Windows ignores case. See how to get the current directory in Python to print where you’re running from.

Which method should you use to check a directory?

MethodReturns for a fileBest for
os.path.isdir(p)FalseQuick checks, older code
Path(p).is_dir()FalseNew code that already uses pathlib
os.path.exists(p)TrueWhen a file or folder is fine
try / exceptNotADirectoryErrorWhen you open the folder next
mkdir(exist_ok=True)FileExistsErrorCreate the folder if it’s missing

Things to keep in mind when checking if a directory exists

  • exists() returns True for files, so use isdir() or is_dir() for folders.
  • Relative paths follow the working directory; build them from __file__ instead.
  • Windows ignores folder-name case, but Linux and macOS don’t, so match the real name.
  • A check followed by an action can race; prefer mkdir(exist_ok=True) or try/except.

When your folder checks pass, these file guides pick up the next step:

Frequently asked questions

How do I check if a directory exists in Python?

Use os.path.isdir("reports") or Path("reports").is_dir(). Both return True only for an existing folder.

What’s the difference between os.path.exists() and os.path.isdir()?

exists() returns True for files and folders. isdir() returns True only for folders.

How do I create a folder only if it doesn’t exist?

Call Path("archive").mkdir(parents=True, exist_ok=True), or os.makedirs("archive", exist_ok=True).

Does os.path.isdir() raise an error for a missing path?

No. It returns False for missing paths, files and paths you can’t access.

Is os.path.isdir() case-sensitive?

It follows the file system. On Windows REPORTS matches reports; on Linux it doesn’t.

Where is os.path.isdir() documented?

See os.path.isdir() in the Python docs.