Python | os.DirEntry.is_dir() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

os.scandir() method of os module yields os.DirEntry objects corresponding to the entries in the directory given by specified path. os.DirEntry object has various attributes and method which is used to expose the file path and other file attributes of the directory entry.

is_dir() method on os.DirEntry object is used to check if the entry is a directory or not.



Note: os.DirEntry objects are intended to be used and thrown away after iteration as attributes and methods of the object cache their values and never refetch the values again. If the metadata of the file has been changed or if a long time has elapsed since calling os.scandir() method. we will not get up-to-date information.

Syntax: os.DirEntry.is_dir(*, follow_symlinks = True)

Parameter:
follow_symlinks: A boolean value is required for this parameter. If the entry is a symbolic link and follow_symlinks is True then the method will operate on the path symbolic link point to. If the entry is a symbolic link and follow_symlinks is False then the method will operate on the symbolic link itself. If the entry is not a symbolic link then follow_symlinks parameter is ignored. The default value of this parameter is True.

Return value: This method returns True if the entry is a directory otherwise returns False.

Code #1: Use of os.DirEntry.is_dir() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.DirEntry.is_dir() method 
  
# importing os module  
import os
  
# Directory to be scanned
# Path
path = "/home / ihritik"
  
# Using os.scandir() method
# scan the specified directory
# and yield os.DirEntry object
# for each file and sub-directory
  
with os.scandir(path) as itr:
    for entry in itr :
        # Check if the entry
        # is directory 
        if entry.is_dir() :
            print("% s is a directory." % entry.name)
        else:
            print("% s is not a directory." % entry.name)

chevron_right


Output:

file.txt is not a directory.
Public is a directory.
Desktop is a directory.
R is a directory.
Music is a directory.
Documents is a directory.
tree.cpp is not a directory.
graph.cpp is not a directory.
Pictures is a directory.
GeeksForGeeks is a directory.
Videos is a directory.
images is a directory.
Downloads is a directory.
abc.txt is not a directory.

Code #2: Use of os.DirEntry.is_dir() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.DirEntry.is_dir() method 
  
# importing os module  
import os
  
# Directory to be scanned
# Path
path = "/home / ihritik"
  
# Using os.scandir() method
# scan the specified directory
# and yield os.DirEntry object
# for each file and sub-directory
  
print("List of all directories in '% s':" % path) 
with os.scandir(path) as itr:
    for entry in itr :
        # Check if the entry
        # is directory 
        if entry.is_dir() :
            # Exclude the directory name
            # starting with '.'  
            if not entry.name.startswith('.') :    
                # Print Directory name    
                print(entry.name)

chevron_right


Output:

List of all directories in path '/home/ihritik':
Public
Desktop
R
Music
Documents
Pictures
GeeksForGeeks
Videos
images
Downloads

References: https://docs.python.org/3/library/os.html#os.DirEntry.is_dir



My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.