Python os.DirEntry.stat() method
Python os.DirEntry.stat() Method
Python The os module’s os.scandir() method produces an os.DirEntry object corresponding to an entry in the directory given by the specified path. An os.DirEntry object has various attributes and methods that expose the file path and other file attributes of a directory entry.
The stat() method on an os.DirEntry object is used to obtain an os.stat_result object for an entry.
Note: os.DirEntry objects are intended to be used and discarded after iteration, as the object’s attributes and methods cache their values and never re-read them. If the file’s metadata has changed, or if a significant amount of time has passed since the os.scandir() method was called, we will no longer have the most up-to-date information.
os.DirEntry.stat() Syntax
os.DirEntry.stat(*, follow_symlinks = True)
os.DirEntry.stat() Parameters
follow_symlinks: This parameter takes a Boolean value. If the entry is a symbolic link and follow_symlinks is True, the method will operate on the path pointed to by the symbolic link. If the entry is a symbolic link and follow_symlinks is False, the method will operate on the symbolic link itself. If the entry is not a symbolic link, the follow_symlinks parameter is ignored. The default value is “True”.
Return Value: This method returns an os.stat_result object for the entry. The following are the attributes of the os.stat_result object:
- st_mode: Indicates the file type and file mode bits (permissions).
- st_ino: Indicates the inode number on Unix and the file index on Windows platforms.
- st_dev: Indicates the device identifier on which the file resides.
- st_nlink: Indicates the number of hard links.
- st_uid: Indicates the user identifier of the file owner.
- st_gid: Indicates the group identifier of the file owner.
- st_size: Indicates the size of the file in bytes.
- st_atime: Indicates the time of the most recent access. It is expressed in seconds.
- st_mtime: Indicates the most recent content modification time. Its unit is seconds.
- st_ctime: Indicates the time of the most recent metadata change on Unix and the creation time on Windows. Its unit is seconds.
- st_atime_ns: Same as st_atime, but the time is expressed as an integer in nanoseconds.
- st_mtime_ns: Same as st_mtime, but the time is expressed as an integer in nanoseconds.
- st_ctime_ns: Same as st_ctime, but the time is expressed as an integer in nanoseconds.
- st_blocks: Indicates the number of 512-byte blocks allocated for the file.
- st_rdev: If it is an inode device, indicates the device type.
- st_flags: Indicates user-defined file flags.
os.DirEntry.stat() example
Use the os.DirEntry.stat() method
# Python program to explain os.DirEntry.stat() method
# importing os module
import os
# Directory to be scanned
#Path
path = "/home/ihritik"
# Print status of all
# files in the above
#specified path
# Using os.scandir() method
# scan the specified directory
# and yield os.DirEntry object
# for each file and sub-directory
print("Status 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("Status of % s:" % entry.name)
print(entry.stat(), "n")
Output:
Status of all files in path '/home/ihritik':
Status of file.txt:
os.stat_result(st_mode=33248, st_ino=801366, st_dev=2056, st_nlink=2, st_uid=1000,
st_gid=1000, st_size=409, st_atime=1566360293, st_mtime=1566287810,
st_ctime=1566291428)
Status of tree.cpp:
os.stat_result(st_mode=33188, st_ino=801364, st_dev=2056, st_nlink=1, st_uid=1000,
st_gid=1000, st_size=820, st_atime=1565604415, st_mtime=1565604415,
st_ctime=1565604415)
Status of graph.cpp:
os.stat_result(st_mode=33188, st_ino=801237, st_dev=2056, st_nlink=1, st_uid=1000,
st_gid=1000, st_size=1729, st_atime=1561515200, st_mtime=1561515069,
st_ctime=1561515069)
Status of abc.txt
os.stat_result(st_mode=33434, st_ino=801196, st_dev=2056, st_nlink=1, st_uid=1000,
st_gid=1000, st_size=0, st_atime=1560204341, st_mtime=1560204341,
st_ctime=1560204349)
Reference: https://docs.python.org/library/os.html#os.DirEntry.stat