×

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

List in Python

What is List in Python In Python, lists are used to store the multiple values in one variable. We can say that list is the collection of similar as well as...

3 minutes read.

How to create a class in python

In any programming language, a class is a user-defined plan or blueprint using which objects or instances of the class are created. You may wonder why we need classes in programming....

5 minutes read.

Python Set add() Method

Python Set add() Method The set.add() method adds the specified element to a set. If the element is already present in the set, it doesn't add it. Syntax set.add(element) Parameter element- This parameter represents the element that...

1 minute read.

Application to get live USD/INR rate 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.

OS Module in Python

What is a module? The Modules are the kind of files that contains python statements and definitions. The module is known by the name of the file followed by the suffix...

8 minutes read.

Python Infinity

Introduction We will discover how to define an infinite number in Python in this tutorial. Infinity, as we all know, is a value that cannot be defined and can either be...

5 minutes read.

Python Euclidean Distance

Euclidean distance is the distance between two points with whatever of dimensions. We are using NumPy library to find and calculate the Euclidean distance. The NumPy library is used for...

1 minute read.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.

Captcha Code in Python with Example

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...

6 minutes read.

Python Permutations and Combinations

In mathematics, we all studied what is meant by permutations and combinations. “Permutations” define the way of arranging the elements in sequential order. “Combinations” mean the way of selecting the...

7 minutes read.

JWT Decode Python

Python's PyJWT package makes it possible to encrypt and decrypt JSON Web Tokens (JWT). JWT is a public, recognized global benchmark for safely expressing demands between two parties (RFC 7519). JSON...

7 minutes read.

Drop() Function in Python

Drop() Function: The python programming language consists of various libraries; some of them are pandas and matplotlib. Data scientists mainly use the pandas library to analyze the data more easily and...

3 minutes read.

Python System Requirements

Introduction As we know, Python is a popular programming language usually used to write scripts for operating systems. It’s handy adequate for utilizing in web development and application design. In this article,...

2 minutes read.

Removing the First Character from the String in Python

String The string is the collection of characters. The strings in python are “immutable”; we cannot change a string once they are created. In python, whatever data we take as input...

4 minutes read.

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...

3 minutes read.

How to build a Virtual Assistant Using Python

What is a virtual assistant? A virtual assistant is a new and very interesting concept in today’s world. When we hear the word “Virtual Assistant”, we can easily visualize “Jarvis or...

8 minutes read.

Python Num2words

Python: 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 is said...

3 minutes read.

Standard GUI Unit Converter using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

12 minutes read.

BOTTLE Python Web Framework

The Bottle is a lightweight WSGI micro web framework for python. It acts like a thin wrapper around a web server where it is distributed as a single file module...

4 minutes read.

Python Examples

In this tutorial, we will see some examples related to python. This will include some basic example questions and their code in Python. Write a program to print “Hello Python”. print(‘Hello Python’) Output Hello...

5 minutes read.