HOWTO · Python

How to Check if Directory Exists using Python

This tutorial demonstrates how to check if a specific directory exists in Python. Learn effective methods using os.path.exists(), os.path.isdir(), and pathlib. Enhance your coding skills and write robust applications with these simple techniques. Perfect for beginners and experienced developers alike.

On this page

To check whether a path names a directory, use os.path.isdir() or, with a Path object, Path.is_dir(). Both distinguish directories from files. The examples below use temporary paths so they do not depend on a folder already being present in your working directory.

Check Whether a Directory Exists with os.path.isdir()

os.path.isdir(path) returns True when the path refers to an existing directory and False when it refers to a file or a path that is not present. It accepts path strings as well as path-like objects. Use it when the question is specifically whether the target is a directory.

Run this example:

import os
from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as temp_dir:
    root = Path(temp_dir)
    directory = root / "reports"
    directory.mkdir()
    file_path = root / "notes.txt"
    file_path.write_text("draft", encoding="utf-8")

    print(os.path.isdir(directory))
    print(os.path.exists(file_path))
    print(os.path.isdir(file_path))
    print((root / "archive").exists())
True
True
False
False

The temporary workspace contains a directory and a regular file, plus a path that has not been created. The first and third checks show why os.path.isdir() is useful for distinguishing a directory from a file. The other calls use the general existence checks discussed below.

Check Whether a Path Exists with os.path.exists()

os.path.exists(path) answers a broader question: does a file or directory exist at this path? It returns True for either kind, so it cannot by itself confirm that a path is a directory. It follows symbolic links, which means a broken symbolic link returns False. For example, the example’s file_path passes os.path.exists() while failing os.path.isdir().

Use this function when either kind of filesystem entry is acceptable, such as checking whether a file is available before reading it. If the later operation requires a directory, prefer the directory-specific check. A check can also become outdated if another process changes the filesystem before your program uses the path; it is not a reservation or a guarantee that a later operation will succeed.

Check Whether a Directory Exists with pathlib

pathlib represents paths as objects and provides methods for asking about them. Call Path.is_dir() for the same directory-specific question answered by os.path.isdir(). Call Path.exists() when either a file or a directory counts as present. Like os.path.exists(), Path.exists() follows symbolic links and returns False for a broken link. In the example, (root / "archive").exists() uses the latter form on a path object.

Choose one path style for a piece of code when possible. pathlib is convenient when you also need to join path components with /, create folders, or open files through methods on Path. The os.path functions remain useful when an existing API or codebase already passes paths as strings.

Choose the Right Directory Check

Choose os.path.isdir() or Path.is_dir() when your next step requires a directory. Choose os.path.exists() or Path.exists() when any existing filesystem entry is sufficient. If you need to tell a file from a directory, check explicitly with is_dir() and, where needed, is_file() rather than inferring the type from general existence.

These functions report the filesystem state they can observe at the time of the check. A path may change before a later read, write, or creation. Handle errors from the operation that matters as well, especially when permissions or concurrent changes can affect the result.

Create a Directory When It Is Missing

If a program needs a directory before saving a file, pathlib can create it directly:

from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as temp_dir:
    output_dir = Path(temp_dir) / "reports"
    output_dir.mkdir(parents=True, exist_ok=True)

With parents=True, missing parent directories are created too. exist_ok=True allows the call to continue when the target directory already exists. If a file occupies the target path, the call still fails; permission and other filesystem errors can also occur. This makes the creation operation suitable for a repeated setup step without relying on a separate check first.

When you need different behavior depending on whether the directory was already present, check with is_dir() and then act, but keep in mind the filesystem may change between those operations. For simply ensuring a directory is available, mkdir(parents=True, exist_ok=True) is shorter and avoids treating a prior check as a guarantee.