| by Arround The Web | No comments

Python os path isdir

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”.

Syntax

os.path.isdir(path)

 
Parameters

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'
print(os.path.isdir(path1))

path2 = r'C:\Users\p\Documents\sample'
print(os.path.isdir(path2))

 
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.

Output


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'
print(os.path.isdir(path1))

 
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.

Output

Conclusion

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.

Share Button

Source: linuxhint.com

Leave a Reply