os.path.isdir() method | Python (original) (raw)

Last Updated : 21 Apr, 2026

os.path.isdir() 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 path = "Documents" print(os.path.isdir(path))

`

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

Syntax

os.path.isdir(path)

Examples

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

Python `

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

`

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

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

Python `

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

`

**Explanation: output is False because "Documents" refers to a directory that does not exist in the script's current working 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"))

`

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