Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

How to Check if a Directory Exists in Python

Use Path.is_dir() or os.path.isdir() to check whether a path points to a directory, with version-specific notes on symlinks and errors.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Path.is_dir() to check whether a path points to a directory. It returns False for a missing path or a path to a regular file, unlike Path.exists(), which can be true for either. If your code already uses os.path, use os.path.isdir() instead.

Check a directory with pathlib

pathlib is a clear choice for modern Python code, especially when you already work with Path objects:

from pathlib import Path

folder = Path("/path/to/check")

if folder.is_dir():
    print("Directory exists")
else:
    print("Directory does not exist")

Path.is_dir() asks whether the path points to a directory, not merely whether an entry exists at that path. The Python 3.12 pathlib documentation says it returns true for a directory or a symbolic link pointing to one, and false for another kind of file.

Use os.path.isdir() in os.path-based code

If the surrounding code uses string paths and os.path helpers, os.path.isdir() provides the same directory-specific check:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import os

folder = "/path/to/check"

if os.path.isdir(folder):
    print("Directory exists")
else:
    print("Directory does not exist")

Python documents os.path.isdir() as checking whether a path is an existing directory. It follows symbolic links. Path-like objects have been accepted since Python 3.6.

Choose the check that fits your code

Check Best fit What it checks Symbolic links
Path(path).is_dir() Code using pathlib or Path objects Whether the path points to a directory Follows the link to its target by default
os.path.isdir(path) Code already using os.path or string-based path helpers Whether the path points to an existing directory Follows the link to its target

Both are standard-library options; the surrounding path-handling style is usually the simplest deciding factor. Neither should be confused with a general existence check.

Why Path.exists() is not enough

Path.exists() returns true if a file or directory exists at the path. It does not establish that the entry is a directory. Use is_dir() when the type matters:

path = Path("/path/to/check")

if path.exists():
    print("Something exists here")

if path.is_dir():
    print("It is a directory")
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Symlinks, missing paths, and error behavior

Symbolic links

Both common checks follow symbolic links by default. A symlink to a directory passes; a broken symlink does not. The follow_symlinks option for Path.is_dir() was added in Python 3.13, so do not rely on that option when supporting earlier versions. See the versioned Python 3.12 pathlib reference for the documented default behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Missing paths and paths that are not directories

A missing path, a broken symlink, or a path to another file type does not pass the directory check. In the Python 3.12 documentation, Path.is_dir() returns false for these cases.

Inaccessible paths and Python version differences

Error handling depends on the Python version. The Python 3.12 documentation says errors other than the ordinary non-directory cases, including permission errors, can propagate. The Python 3.15 prerelease documentation records a change made in Python 3.14: pathlib status checks return false for invalid, inaccessible, or missing paths instead of raising OSError. If your program needs to tell an inaccessible path from a missing one, inspect it with Path.stat() and handle the relevant errors explicitly.

Practical choice

  • Use Path(path).is_dir() for pathlib-based code and modern path composition.
  • Use os.path.isdir(path) when it fits the helpers already used in your codebase.
  • Use Path.exists() only when you need to know whether some filesystem entry exists, regardless of whether it is a file or directory.
  • Account for your supported Python versions if inaccessible paths must be distinguished from missing ones.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.