Open In App

Python | os.DirEntry.is_file() method

Improve
Improve
Like Article
Like
Save
Share
Report

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_file() method on os.DirEntry object is used to check if the entry is a file 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_file(*, 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 file otherwise returns False.

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




# Python program to explain os.DirEntry.is_file() 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 files in path '% s':" % path) 
with os.scandir(path) as itr:
    for entry in itr :
        # Check if the entry
        # is a file 
        if entry.is_file() :
            # Print file name    
            print(entry.name)


Output:

List of all files in path '/home/ihritik':
file.txt
tree.cpp
graph.cpp
abc.txt

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




# Python program to explain os.DirEntry.is_file() method 
  
# importing os module  
import os
  
# Directory to be scanned
# Path
path = "/home / ihritik"
  
  
# Print all file names
# starting with letter 'g'
# in above specified path
  
# 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 a file 
        if entry.is_file() :
            if entry.name.startswith('g'):
                # Print file name    
                print(entry.name)


Output:

graph.cpp

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



Last Updated : 28 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads