Python os.path.samestat() Method



The Python os.path.samestat() method is used to check whether the status information of two paths are equivalent. It compares the file metadata, such as file mode, size, and timestamps, to determine if the two paths point to the same file or directory in the file system.

If the stat information for both paths is same, the method returns True, indicating that the paths point to the same file or directory. If the stat information differs between the two paths, it returns False.

Syntax

Following is the basic syntax of the Python os.path.samestat() method −

os.path.samestat(stat1, stat2)

Parameter

This method accepts the following parameters −

  • stat1 − This is the status information of the first path.

  • stat2 − This is the status information of the second path.

Return Value

The method returns a boolean value "True" or "False". It returns True if the stat information for both paths is same, otherwise it returns False.

Example

In the following example, we are comparing the status information of two same files using the samestat() method −

import os
path1 = os.stat("C://Users//Lenovo//Desktop//file.txt")
path2 = os.stat("C://Users//Lenovo//Desktop//file.txt")
result = os.path.samestat(path1, path2)
print("The result obtained is:",result) 

Output

Following is the output of the above code −

The result obtained is: True

Example

Here, we compare the status information of two different files using the samestat() method −

import os
path1 = os.stat("C://Users//Lenovo//Desktop//file.txt")
path2 = os.stat("C://Users//Lenovo//Desktop//file2.txt")
result = os.path.samestat(path1, path2)
print("The result obtained is:",result)   

Output

Output of the above code is as follows −

The result obtained is: False

Example

Now, we open the same file "file.txt" twice with different file descriptors "fd1" and "fd2". Then, we get the stat buffers for both file descriptors and check if they are the same −

import os

# Opening the same file multiple times
fd1 = os.open("C://Users//Lenovo//Desktop//file.txt", os.O_RDONLY)
fd2 = os.open("C://Users//Lenovo//Desktop//file.txt", os.O_RDONLY)

# Getting stat buffers for both file descriptors
stat1 = os.fstat(fd1)
stat2 = os.fstat(fd2)

# Checking if stat buffers are the same
result = os.path.samestat(stat1, stat2)
print("The result obtained is:",result)  

Output

We get the output as shown below −

The result obtained is: True

Example

In this example, we get the stat buffers for the same file "file.txt" at different timestamps −

import os
import time

# Getting stat buffers for the same file at different times
stat1 = os.stat("C://Users//Lenovo//Desktop//file.txt")
# Wait for 1 second
time.sleep(1)  
stat2 = os.stat("C://Users//Lenovo//Desktop//file.txt")

# Checking if stat buffers are the same
result = os.path.samestat(stat1, stat2)
print("The result obtained is:",result)  

Output

The result produced is as follows −

The result obtained is: True
os_path_methods.htm
Advertisements