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.
name attribute on
os.DirEntry object is used to get the entry’s base filename, relative to the path argument used in
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.
Return value: This attribute returns an string value which represents the entry’s base filename.
Code #1: Use of
Base filename of all directory entry in '/home/ihritik': Public Desktop R foo.txt graph.cpp tree.cpp Pictures abc.py file.txt Videos images Downloads GeeksforGeeks Music Documents
Code #2: Use of
All files and directory whose name starts with letter 'D' in '/home/ihritik': Desktop Documents Downloads
- Python | os.DirEntry.path attribute
- Python | sympy.Interval().inf attribute
- Python | Summation of list as tuple attribute
- Important differences between Python 2.x and Python 3.x with examples
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- Python | Index of Non-Zero elements in Python list
- Python | Merge Python key values to list
- Python | Sort Python Dictionaries by Key or Value
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to Python Libraries
- Python | Add Logging to a Python Script
- Python | Visualizing O(n) using Python
- Python | a += b is not always a = a + b
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.