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.
stat() method on
os.DirEntry object is used to get os.stat_result object for an entry.
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: os.DirEntry.stat(*, 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 an os.stat_result object for the entry. Following are the attributes of os.stat_result object:
- st_mode: It represents file type and file mode bits (permissions).
- st_ino: It represents the inode number on Unix and the file index on Windows platform.
- st_dev: It represents the identifier of the device on which this file resides.
- st_nlink: It represents the number of hard links.
- st_uid: It represents the user identifier of the file owner.
- st_gid: It represents the group identifier of the file owner.
- st_size: It represents the size of the file in bytes.
- st_atime: It represents the time of most recent access. It is expressed in seconds.
- st_mtime: It represents the time of most recent content modification. It is expressed in seconds.
- st_ctime: It represents the time of most recent metadata change on Unix and creation time on Windows. It is expressed in seconds.
- st_atime_ns: It is same as st_atime but the time is expressed in nanoseconds as an integer.
- st_mtime_ns: It is same as st_mtime but the time is expressed in nanoseconds as an integer.
- st_ctime_ns: It is same as st_ctime but the time is expressed in nanoseconds as an integer.
- st_blocks: It represents the number of 512-byte blocks allocated for file.
- st_rdev: It represents the type of device, if an inode device.
- st_flags: It represents the user defined flags for file.
Code: Use of
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)
- class method vs static method in Python
- Python | os.dup() method
- Python | next() method
- Python | set() method
- Python | os.strerror() method
- Python | hasattr() method
- Python | os.openpty() method
- Python | os.fork() method
- Python | os.getgrouplist() method
- Python | os.setreuid() method
- Python | os.pipe() method
- Python | os.getpgrp() method
- Python | os.pwrite() method
- Python | os.setregid() method
- Python | os.getsid() method
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.