+

Python | os.path.samestat () method

os.path.samestat() in Python is used to check if a given tuple stat refers to the same file or not. Tuple Stat — it is an object that can be returned by the os.fstat (), os.lstat (), or os.stat () methods. It represents the state of the path.

Syntax: os.path.samestat (stat1, stat2)

Parameter:
stat1 : A stat tuple.
stat2 : A stat tuple.

Return Type: This method returns a Boolean value of class bool. This method returns True if both stat tuple refer to the same file otherwise returns false.

Code: Use os.path.samestat () method to check if whether these tuples refer to the same file.

# Python program to explain the os.path.samestat () method

 
# import of the os module

import os

 
# Path

path1 = "/ home / ihritik / Desktop / file.txt "

  
# Get status
# given path # using the os.stat () method

stat1 = os.stat (path1)

 
# Path

path1 = " / home / ihritik / Desktop / file.txt "

  
# open the file and get
# the file descriptor associated
# with this

fd = os. open (path1, os.O_RDONLY)

 
# Get status
# file associated with
#descript fd file op
# using the os.fstat () method

stat2 = os.fstat (fd)

 
# Check
# stat1 and stat2 are relevant
# to the same file

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

 
# Print the result

print (sameFile)

 
os.close (fd)

 

 
# Path

path3 = "/ home / ihritik / Desktop / file.txt"

 
# Get status
# above path
# using the os.lstat () method

stat3 = os.lstat (path3)

 
# Check
# stat1 and stat3 link
# to the same file

sameFile = os.path.samestat (stat1, stat3 )

 
# Print result

print (sameFile)

  

 
# Path

path4 = "/ home / ihritik / Documents / file.txt"

 
# Get status
# above path
# using the os.stat () method

stat4 = os.stat (path4)

 
# Check
# stat1 and stat4 link
# to the same file

sameFile = os.path.samestat (stat1, stat4)

  
# Print print result

print (sameFile)

Exit :

 True True False 

Link: https://docs.python.org/3/library/os.path .html

Get Solution for free from DataCamp guru