×

os.stat() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature that depends on the Operating System. This module's main feature is interacting with the file and path directory. In this article, we are going to learn about os.path.stat features of Python. So, before understanding what Python's stat() module is, we need to understand a bit about the directory tree. 

What is the directory tree?

The files in the UNIX system are organized similarly to in a tree data structure, and that's why it is known as a directory tree. At the very top of the file system, there is a directory called "root," which is represented by "/," and all other files come under this.

The os.stat() method

In Python, the os.stat() method provides the feature to make a system call for the specific path. With the help of this method, we can get the status of the specific path. 

Syntax for os.stat() method

We can implement the os.stat() method with the help of the following syntax. 

os.stat(path)

Parameter for os.stat() method

There are some parameters that are used in the os.stat() method. These parameters are as follows:

  1. Path: It represents the valid path for the file.
  2. Return type: With this return type's help, the program returns stat_result for the object class. The os.stat_results shows the status for the specific path. There are some attributes that are provided by the stat_result. 
  • St_mode: It shows the permission and type of the file.
  • St_ino: It shows the file index number on the window.
  • St_dev: It shows the identifier number of the device in which the file is stored.
  • St_nlink: It shows the number of hard links present in it. 
  • St_uid: It shows the user identification number of the user file.
  • St_gid: It shows the group identification number of the user file.
  • St_size: It shows the size of the file in bytes.
  • St_atime: It shows the most access time of the file in seconds.
  • St_mtime: It shows the most file modification time of the file in seconds.
  • St_ctime:  It shows the most recent time of metadata change of the file UNIX and file creation time on the window in seconds.
  • St_atime_ns: The function of st_atime_ns is similar to st_atime, but it displays all the information in nanoseconds.
  • st_mtime_ns: The function of st_mtime_ns is similar to st_atime, but it displays all the information in nanoseconds.
  • st_ctime_ns: The function of st_ctime_ns is similar to st_atime, but it displays all the information in nanoseconds.
  • St_blocks: It shows the number of allocations to the file.
  • St_rdev: It shows the type of the device if the device type is an inode device.
  • St_flags: It shows the user define flag for system files.

Example 1:

# Python program to explain os.stat() method
         
# importing os module
import os
 
# path
path = '/home/User/Documents/demo.txt'
 
# Get the status of
# the specified path
status = os.stat(path)
 
 
# Print the status
# of the specified path
print(status)

Output:

The os.stat() method in Python

Example 2:

# !/usr/bin/python


import os, sys


# showing stat information of file "a2.py"
statinfo = os.stat('a2.py')


print statinfo

Output:

The os.stat() method in Python

Example 3:

import stat
import os, time
 
st = os.stat("samples/sample.txt")
 
print "mode", "=>", oct(stat.S_IMODE(st[stat.ST_MODE]))
 
print "type", "=>",
if stat.S_ISDIR(st[stat.ST_MODE]):
    print "DIRECTORY",
if stat.S_ISREG(st[stat.ST_MODE]):
    print "REGULAR",
if stat.S_ISLNK(st[stat.ST_MODE]):
    print "LINK",
print
 
print "size", "=>", st[stat.ST_SIZE]
 
print "last accessed", "=>", time.ctime(st[stat.ST_ATIME])
print "last modified", "=>", time.ctime(st[stat.ST_MTIME])
print "inode changed", "=>", time.ctime(st[stat.ST_CTIME])

Output:

The os.stat() method in Python

Example 4:

import os
import sys
 
path ="/home/File.txt"
fd = os.open(path, os.O_RDWR|os.O_CREAT)
os.close(fd)
file_info = os.stat(path)
 
print ("The device ID is:", file_info[2])
print ("The user ID is:", file_info[4])

Output:

The os.stat() method in Python

Explanation:

The information returned by the os.stat() method is in the form of an array, starting from the index number 0. Some elements of this method can be retrieved by specifying the index number.


Related Topics

Application to Search Installed Application using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Python Set issuperset() method

Python Set issuperset() method The set.issuperset() method in Python returns a boolean value True if all items in the specified set exists in the original set, else it returns False. Syntax set.issuperset (set1) Parameter set- This parameter represents the...

2 minutes read.

Python String startswith() method

Python String startswith() method The string.startswith() method in Python returns a boolean value ‘True’ if the given string starts with the prefix, otherwise it returns False. Syntax startswith(prefix[, start[, end]]) Parameter prefix: This parameter signifies the value to check. start(optional):...

1 minute read.

Python delattr() function

Python delattr() function The delattr() function in Python is used to delete the named attribute from the object, with the prior permission of the object. Syntax delattr(object, name) Parameter object : This parameter represents the object from which...

1 minute read.

Fsolve in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Yield Statement In Python

The generators are defined by using the yield statement in Python. Generally, it converts a normal Python function into a generator.  The yield statement hauls the function and returns back the...

2 minutes read.

Python Modules

Python Modules The Python module is a collection of definition and statements. It can contain functions, classes, and variables.  Combining the related code into a module makes the code easier to understand and use....

5 minutes read.

Python Operators

Operators in Python programming An operator is a symbol that operates on one or more operands. An operand is a value or a variable on which we perform the operation....

5 minutes read.

Python RegEx

Python RegEx (python regular expressions) is a concept of writing and finding expressions in a pool of characters easily. A Regular expression (RegEx) is a set of characters that defines search...

10 minutes read.

Python Write Excel File

Python Write Excel File The Python xlwt module is used to write an excel file and perform multiple operations on it. It can be used to write text, numbers, and formulas for multiple...

3 minutes read.

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to...

4 minutes read.

Python List Methods

Python List Methods Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects: Methods Explanation append The list.append() method...

3 minutes read.

Python coding platform

Python is a popular general-purpose programming language with many applications. High-level data structures, datatypes, dynamic binding, and many other features make it useful for both designing complex applications and "glue...

6 minutes read.

Python callable() Function

Python callable() Function The callable() function returns a boolean value ‘True’ if the specified object is callable, else it returns False. Syntax callable(object) Parameter Object:  The object parameter represents the value to test if it is callable...

1 minute read.

Spotify API in Python

The application programming interface is referred to as API. In essence, an API serves as a layer of communication or, as the name suggests, an interface that enables systems to...

3 minutes read.

Yield vs Return in Python

In this article, you will learn about " Yield " and " Return " in Python. You will also understand the difference between " Yield " and " Return "...

6 minutes read.

Loan calculator using Tkinter in Python

Tkinter: Tkinter, part of all common Python distributions, is the de facto method for creating Graphical User Interfaces (GUIs) in Python. The only framework included in the Python standard library is...

4 minutes read.

Python IDE names

Python is one of the most renowned programming languages. It has different execution conditions and a great many compilers to execute the python programs, e.g., PyCharm, PyDev, Jupyter Notebook, Visual...

6 minutes read.

Struct Module in Python

What is a structure in Python? An entity that holds data in form of a structure is called a data structure. In Python, the data structure includes tuples, lists, dictionaries, and...

4 minutes read.

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.