Python os.path.samestat() Method

Python os.path.samestat() Method

  1. Syntax of Python os.path.samestat() Method
  2. Example 1: Use the os.path.samestat() Method to Check if the stat Tuples Refer to the Same File
  3. Example 2: Use the os.getcwd() Method With the os.path.samestat() Method
  4. Example 3: Make Your Own os.path.samestat() Method

Python os.path.samestat() method is an efficient way to check whether the stat tuples refer to the same path file or not. A stat tuple is an object that can be returned by any of the Python methods: os.fstat(), os.lstat() or os.stat().

Syntax of Python os.path.samestat() Method

os.path.samestat(stat1, stat2)

Parameters

stat1 A tuple representing the status of a path.
stat2 A second tuple representing the status of a path.

Return

The return type of this method is a Boolean value representing true if both the tuples refer to the same directories.

Example 1: Use the os.path.samestat() Method to Check if the stat Tuples Refer to the Same File

import os

fd = os.open( "File.txt", os.O_RDWR|os.O_CREAT )

stat1 = os.fstat(fd)

path2 ="/home/File.txt"

stat2 = os.stat(path2)

similarity = os.path.samestat(stat1, stat2)

print("Are the tuples pointing to the same path? ", similarity)

os.close(fd)

Output:

Are the tuples pointing to the same path?  True

In the background, this method uses the samefile() and sameopenfile() methods to compare the stat tuples.

Example 2: Use the os.getcwd() Method With the os.path.samestat() Method

import os

fd = os.open( "File.txt", os.O_RDWR|os.O_CREAT )

stat1 = os.fstat(fd)

path2 = os.path.join(os.getcwd(), "File.txt")

stat2 = os.stat(path2)

similarity = os.path.samestat(stat1, stat2)

print("Are the tuples pointing to the same path? ", similarity)

os.close(fd)

Output:

Are the tuples pointing to the same path?  True

This method returns true if the tuples entered are for an original file and its shortcut created in a different directory.

Example 3: Make Your Own os.path.samestat() Method

import os

def os_path_samestat(tuple1, tuple2):

  stat1 = os.stat(tuple1) if os.path.isfile(tuple1) else None
  if not stat1:
    return False
  stat2 = os.stat(tuple2) if os.path.isfile(tuple2) else None
  if not stat2:
    return False
  return (stat1.st_dev == stat2.st_dev) and (stat1.st_ino == stat2.st_ino)

fd = os.open( "File.txt", os.O_RDWR|os.O_CREAT )

tuple1 ="/home/File.txt"

tuple2 = os.path.join(os.getcwd(), "File.txt")

similarity = os_path_samestat(tuple1, tuple2)

print("Are the tuples pointing to the same path? ", similarity)

Output:

Are the tuples pointing to the same path?  True

In some versions of Python, the original os.path.samestat() method might not be available so that you can make your method.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method