os.scandir() of the os module
os.DirEntry objects corresponding to the entries in the directory specified by the specified path. The
os.DirEntry object has various attributes and a method that is used to provide the file path and other attributes of the directory entry file.
is_file() of the object
os.DirEntry is used to check if the entry is a file or not.
os.DirEntry are meant to be used and thrown away after iteration, as the object’s attributes and methods cache their values and never over-execute the values again. If the file metadata has changed, or if a long time has passed since the os.scandir () method was called. we will not receive up-to-date information.
Syntax: os.DirEntry.is_file (*, follow_symlinks = True)
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: Using the
List of all files in path ’/ home / ihritik’: file.txt tree.cpp graph.cpp abc.txt
Code # 2: Using method
os .DirEntry.is_file ()