os.path.isdir() method - Python

Last Updated : 13 Jan, 2026

os.path.isdir() is a Python method used to check whether a given file system path refers to an existing directory. It returns True if the path points to a directory; otherwise, it returns False. This method also follows symbolic links, meaning a symbolic link pointing to a directory is treated as a directory.

This example checks whether a given path exists and is a directory.

Python
import os
p = "Documents"
print(os.path.isdir(p))

Output
False

Explanation: os.path.isdir(p) checks if p refers to an existing directory.

Syntax

os.path.isdir(path)

  • Parameters: path - A string or path-like object representing a file system path.
  • Return: Returns True if the path is an existing directory, otherwise False.

Example 1: This example checks whether a file path refers to a directory.

Python
import os
p = "file.txt"
r = os.path.isdir(p)
print(r)

Output
False

Explanation: os.path.isdir(p) returns False because p points to a file, not a directory.

Example 2: This example verifies whether a directory path exists and is a directory.

Python
import os
p = "Documents"
r = os.path.isdir(p)
print(r)

Output
False

Explanation: os.path.isdir(p) returns True because p is an existing directory.

Example 3: This example checks whether a symbolic link pointing to a directory is treated as a directory.

Python
import os

os.mkdir("testdir")
os.symlink("testdir", "linkdir")

print(os.path.isdir("testdir"))
print(os.path.isdir("linkdir"))

Output
True
True

Explanation: os.path.isdir("linkdir") returns True because the symbolic link points to a directory.

Comment

Explore