Open In App

Python | os.stat() method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

OS comes under Python standard utility modules. This module provides a portable way of using operating system-dependent functionality. os.stat() method in Python performs stat() system calls on the specified path. This method is used to get the status of the specified path.

os.stat() Method Syntax in Python

Syntax: os.stat(path)

Parameter: path: A string or bytes object representing a valid path

Return Type: This method returns a ‘stat_result’ object of class ‘os.stat_result’ which represents the status of specified path. The returned ‘stat-result’ object has following attributes:

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

Python os.stat() Method Example

Below are some examples by which we can understand how to get file information using os.stat() in Python:

Get Stat Information of a File Using os.stat() in Python

In this example code utilizes the `os.stat()` method of OS module to retrieve status information for the file ‘/home/User/Documents/file.txt’. The obtained details, including file size and modification time, are stored in the `status` variable. The final line prints the status information.

Python3




# importing os module
import os
 
# path
path = '/home/User/Documents/file.txt'
 
# Get the status
status = os.stat(path)
 
# Print the status
print(status)


Output:

os.stat_result(st_mode=33188, st_ino=795581, st_dev=2056, st_nlink=1, st_uid=1000,
st_gid=1000, st_size=243, st_atime=1531567080, st_mtime=1530346690, st_ctime=1530346690)

Measure the File Size in Python with OS Module

In this example, the file is opened in binary mode ('rb'), and the file pointer is moved to the end using file.seek(0, os.SEEK_END). The file size is then obtained using file.tell(). This approach provides an alternative way to calculate the file size.

Python3




import os
 
file_path = 'example.txt'
 
# Open the file in binary mode and seek to the end
with open(file_path, 'rb') as file:
    file.seek(0, os.SEEK_END)
    file_size = file.tell()
 
print(f"The size of the file '{file_path}' is {file_size} bytes.")


Output:

The size of the file 'example.txt' is 12345 bytes.


Last Updated : 12 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads