Open In App

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

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




# 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())


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


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