×

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

Check Letter in a String Python

Checking a specific letter or a character in a given string or a string which is taken as an input from the user is possible in Python in broadly three...

3 minutes read.

Python Image Processing

What is an Image? Images are the pictures that will define the world, and it has their own story, and consists of information about them and these are useful in many...

9 minutes read.

Python Redis Example

Introduction: Redis, representing Distant Word reference Server, is a kind of key-esteem NoSQL server. Redis is intended to help circle capacity for determination, holding information after power is stopped, and stores information...

5 minutes read.

Python filter() function

Python filter() function The filter() function constructs an iterator from those elements of iterable for which the parameter ‘function’ returns a Boolean value true. Syntax filter(function, iterable) Parameter function: This parameter represents a function to be run for each item in the...

1 minute read.

Python pow() Function

Python pow() Function The pow() function in Python return the parameter ‘x’ to the power ‘y’ and if the parameter ‘z’ is present, it returns x to the power y, modulo z (computed more efficiently than pow(x, y) % z). Syntax pow(x, y[, z]) Parameter x: This parameter represents the base...

1 minute read.

Python Data Types

Python Data Types: The variable in Python is used to store values, and they can hold different values. Each variable in Python has its own data-type. Since Python is a...

13 minutes read.

Python vs Java

There are a lot of programming languages out there, and every day, a new programming language is developing in some parts of the world. Each programming language is a mixture...

5 minutes read.

How to Convert String to List In Python?

How to Convert String to List In Python? We all are familiar with what strings and lists are, let us have a quick revision on them- Strings are a sequence of characters...

4 minutes read.

OSError in Python

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

4 minutes read.

Python String title() method

Python String title() method The string.title() method in Python returns a string where the first character in every word is upper case. If the word contains a number or a symbol,...

1 minute read.

How to Sort a String in Python?

The characters in the string are sorted or put in alphabetical order using the sort string function in Python. Python has built-in techniques for sorting strings available. Since we occasionally...

6 minutes read.

Programs for Printing Pyramid Patterns in Python

<!-- wp:paragraph --><p>Python supports printing patterns using basic for loops. The number of rows is handled by the first outer loop, while the number of columns is handled by the...

9 minutes read.

Python List reverse() method

 Python List reverse() method The list.reverse () method in Python reverses the elements of the list in place. Syntax list.reverse() Parameter NA Example 1 # Python program explaining # the list.reverse() method weekList = ['Sun','Thurs','Mon','Fri', 'Tues', 'Wed'] print("Actual week list: ",weekList) #...

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

Add Dictionary to Dictionary in Python

Dictionary in python: Python's execution of a data model, known more commonly as an implicit array, is a dictionary. A dictionary is made up of a group of key-value pairs. Each...

4 minutes read.

Filter List in Python

Python: Python is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a user-friendly programming...

3 minutes read.

Python Control Flow Statements

This article aims to introduce you to what control flow statements are in general and Control Flow Statements in Python programming Language, the Importance of control flow statements and look...

3 minutes read.

What Does the Percent Sign (%) Mean in Python?

In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand. Example: value1 = 8 value2 = 2 remainder =...

4 minutes read.

Math Module in Python

In this article, you are going to learn everything in detail about the “ math “ module in Python. We can normally work with general operations using python without importing or...

16 minutes read.

Returning Multiple Values in Python

Python is considered a general-purpose programming language; it is a high-level programming language that is not much difficult and easier to learn. It is rich in libraries that can be...

3 minutes read.