Python os.lstat() Method

Python os.lstat() method returns information about the symbolic link. It is similar to the os.stat() method, but unlike os.stat(), os.lstat() does not follow symbolic links.

This method returns information about the file, directory, or symbolic link specified in the path. When the path is a symbolic link, it returns the information about the link itself, not the file it points to.

Syntax

os.lstat(path, *, dir_fd=None)

Parameters

Name Description
path (string) The path to the file, directory, or symbolic link.
dir_fd (optional) A file descriptor refers to a directory.

Return value

It returns an os.stat_result object, which contains several attributes about the file or link, like size, modification time, etc.

The returned os.stat_result object has attributes like:

  1. st_mode (file mode)
  2. st_ino (inode number)
  3. st_dev (device)
  4. st_nlink (number of hard links)
  5. st_uid (user ID of owner)
  6. st_gid (group ID of owner)
  7. st_size (size of a file, in bytes)
  8. st_atime (time of last access)
  9. st_mtime (time of last modification)
  10. st_ctime (time of last status change)

Example 1: Basic usage

Here is a symlink file, “linked-file” to get the details:

Screenshot of symlink file on mac

import os

# Getting stats of a symbolic link
link_stat = os.lstat('linked-file')
print(link_stat)

Output

os.stat_result(st_mode=41453, st_ino=109734185, 
               st_dev=16777234, st_nlink=1, 
               st_uid=501, st_gid=20, 
               st_size=47, st_atime=1705592532, 
               st_mtime=1705592532, st_ctime=1705592532)

Example 2: Comparing os.stat() and os.lstat()

import os

# Comparing stat and lstat
normal_stat = os.stat('linked-file')
link_stat = os.lstat('linked-file')

print("os.stat():", normal_stat)
print("os.lstat():", link_stat)

Output

os.stat(): os.stat_result(st_mode=16877, st_ino=93318505, st_dev=16777234, 
                          st_nlink=36, st_uid=501, st_gid=20, 
                          st_size=1152, st_atime=1705593067, st_mtime=1705593067, 
                          st_ctime=1705593067)

os.lstat(): os.stat_result(st_mode=41453, st_ino=109734185, st_dev=16777234, 
                           st_nlink=1, st_uid=501, st_gid=20, 
                           st_size=47, st_atime=1705592532, st_mtime=1705592532, 
                           st_ctime=1705592532)

Example 3: Accessing specific attributes

import os

link_stat = os.lstat('linked-file')
print(f"Link Size: {link_stat.st_size} bytes")
print(f"Last Modified: {link_stat.st_mtime}")

Output

Link Size: 47 bytes

Last Modified: 1705592532.4994438

Example 4: Error handling

import os

try:
  link_stat = os.lstat('nonexistent_link')
except FileNotFoundError as error:
  print(f"Error: {error}")

Output

Error: [Errno 2] No such file or directory: 'nonexistent_link'

This method is essential when dealing with file systems extensively using symbolic links and when distinguishing between the link and its target is important.

Leave a Comment

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