Jump to content

Python os path isdir

Linux Hint

Recommended Posts

Python offers many built-in modules which deliver different kinds of functionalities and tools. The “os” is one such module in Python that interacts with the operating system and performs common tasks such as creating, deleting, moving, and renaming files and directories. The “os.path.isdir()” function accepts a path as an argument and retrieves a Boolean value indicating whether the path corresponds to an existing directory or not. This can be useful for validating user input, checking the status of a directory, or performing different actions based on the path type.

This write-up will deliver a thorough guide on Python’s “os.path.isdir()” method via the below contents:

What is the “os.path.isdir()” Method in Python?

In Python, the “os.path.isdir()” method checks whether a given path is an existing/current directory or not. The method will retrieve “True” if the particular path points to a directory via a symbolic link. Otherwise, it returns “False”.




In the above syntax, the “path” parameter specifies the path-like object corresponding to the file system path.

Return Value

The “os.path.isdir()” method retrieves “True” or “False”. If the specified path already exists, this method retrieves “True”; otherwise, it retrieves/returns “False”.

Example 1: Applying the “os.path.isdir()” Method to Check Whether the Specified Directory Path Exists or Not

The below code is utilized to verify whether the defined path is present or not:

import os.path
path1 = r'C:\Users\p\Documents\program'

path2 = r'C:\Users\p\Documents\sample'

In the above code:

    • The “os.path” module is imported at the start.
    • The path value is assigned to the variables named “path1” and “path2”, respectively.
    • The “os.path.isdir()” method takes the “path” as an argument in both cases to retrieve the corresponding Boolean value “True” or “False” based on the existence of the specified directory i.e., “program” and “sample” in the path.


Here, the Boolean values indicate whether the specified directory path exists or not.

Example 2: Applying the “os.path.isdir()” Method to Check Whether the Specified Directory Exists or Not in the Current Working Directory

The following code is used to check the directory existence in the current working directory utilizing its name:

import os.path
path1 = 'Python'

In the above code:

    • The “os.path” module is imported at the start.
    • The directory name that needs to be checked is assigned to a variable named “path1”.
    • The “os.path.isdir()” method takes the directory name i.e., “Python” as an argument and retrieves “True” or “False” based on the existence of a directory in the current working directory.




The “os.path.isdir()” method of the “os” module is utilized to check whether a given/specified path is an existing/current directory or not. In the case of symbolic links, the method will return “True“. This method takes only the name of the directory as an argument and retrieves “True” or “False” based on its existence. This Python guide presented an in-depth tutorial on the “os.path.isdir()” method using numerous examples.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...