Python os.stat() Method

Python os.stat() method performs stat() system calls on the specified path and retrieves various statistics about a file or a directory.

This method returns an object that contains data like size, permissions, last modified date, and other details about the file or directory.

Syntax

os.stat(path, *, dir_fd=None, follow_symlinks=True)

Parameters

Name Description
path (string) It is a path to the file or directory.
dir_fd (optional) A file descriptor refers to a directory.
follow_symlinks (optional, boolean) If True, symbolic links are followed, and the stat of the target file is returned; if False, the stat of the symbolic link itself is returned.

Return value

It returns an os.stat_result object, which has the following attributes:

  1. st_mode: It represents protection bits.
  2. st_ino:  It represents the inode number.
  3. st_dev: It represents the device.
  4. st_nlink: It represents a number of hard links.
  5. st_uid: It represents the user ID of the owner.
  6. st_gid: It represents the group ID of the owner.
  7. st_size: It represents the size of a file in bytes.
  8. st_atime: It represents the time of most recent access.
  9. st_mtime: It represents the time of the most recent content modification.
  10. st_ctime: (the “ctime” – on Unix, it’s the time of most recent metadata change, on Windows, the creation time).

Example 1: Basic usage

We will retrieve statistics from the “data.txt” file.

Screenshot of data.txt file in current working directory

import os

# Getting stats of a file
file_stat = os.stat('data.txt')
print(file_stat)

Output

os.stat_result(st_mode=33188, st_ino=94774110, 
              st_dev=16777234, st_nlink=1, 
              st_uid=501, st_gid=20, st_size=59, 
              st_atime=1705427282, st_mtime=1701152407, 
              st_ctime=1705427282)

Example 2: Accessing specific attributes

import os

# Getting stats of a file
file_stat = os.stat('data.txt')

print(f"File Size: {file_stat.st_size} bytes")
print(f"Last Modified: {file_stat.st_mtime}")
print(f"Permissions: {file_stat.st_mode}")

Output

File Size: 59 bytes
Last Modified: 1701152407.343114
Permissions: 33188

Example 3: Working with symbolic links

import os

# Getting stats without following symbolic links
file_stat = os.stat('data_link', follow_symlinks=False)
print(file_stat)

By default, this method follows symbolic links, returning information about the file the link points to. You can change this behavior with the follow_symlinks argument.

Example 4: Error Handling

import os

try:
  file_stat = os.stat('nonexistent_file.txt')
except FileNotFoundError as error:
  print(f"Error: {error}")

Output

Error: [Errno 2] No such file or directory: 'nonexistent_file.txt'

Example 5: Directory statistics

import os

# Getting stats of a directory
dir_stat = os.stat('dir1')
print(dir_stat)

Output

os.stat_result(st_mode=16877, st_ino=98903933, 
               st_dev=16777234, st_nlink=4, st_uid=501, 
               st_gid=20, st_size=128, st_atime=1705084639, 
               st_mtime=1705084639, st_ctime=1705084639)

That’s all!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.