Python | os.DirEntry.inode() 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.

inode() method on os.DirEntry object is used to get the inode number of a entry.



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: inode()

Parameter: No parameter is required

Return type: This method returns an integer value which represents the inode number of the entry.

Code: Use of os.DirEntry.inode() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.DirEntry.inode() method 
  
# importing os module  
import os
  
  
# Directory to be scanned
path = os.getcwd()
  
# Using os.scandir() method
# scan the specified directory
# and yield os.DirEntry object
# for each file and sub-directory
  
print("Directory entry name and their inode number"
with os.scandir(path) as itr:
    for entry in itr :
        # Exclude the entry name
        # starting with '.'  
        if not entry.name.startswith('.') :
            # print entry name
            # and entry's inode() number 
            print(entry.name, " :", entry.inode())

chevron_right


Output:

Directory entry name and their inode number
Public  : 786500
Desktop  : 786497
R  : 1969824
foo.txt  : 801099
graph.cpp  : 801237
tree.cpp  : 801364
Pictures  : 786503
abc.py  : 801140
file.txt  : 801366
Videos  : 786504
images  : 1969766
Downloads  : 786498
geeksforgeeks  : 2097180
Music  : 801428
Documents  : 786501

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



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.