Python os.path.samefile() Method



The Python os.path.samefile() method is used to check whether two specified paths refer to the same file or directory in the file system. It compares the actual files or directories referenced by the paths, rather than just their path strings.

If both paths point to the same file or directory (even if they are specified using different path strings), the method returns True. If the paths do not refer to the same file or directory, it returns False.

Symbolic links are resolved to their target paths before comparison, so if both paths resolve to the same file or directory, the samefile() method returns True.

Syntax

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

os.path.samefile(path1, path2)

Parameter

This method accepts the following parameters −

  • path1 − This is a string representing the first path name to be compared.

  • path2 − This is a string representing the second path name to be compared.

Return Value

The method returns a boolean value "True" or "False". It returns True if both paths refer to the same file or directory, otherwise it returns False.

Example

In the following example, we are checking if both "path1" and "path2" point to the same file using the samefile() method −

import os
path1 = "C://Users//Lenovo//Desktop//file.txt"
path2 = "C://Users//Lenovo//Desktop//file.txt"
result = os.path.samefile(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 are checking whether two different paths "path1" and "path2" point to the same file −

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

Output

Since they have different paths, even if the file names are similar, the output will be False as shown below −

The result obtained is: False

Example

This example checks if "path1" and "path2" point to the same file. The method resolves the relative path to the absolute path and compares them −

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

Output

We get the output as shown below −

The result obtained is: True

Example

Now, we are checking if "path1" and "path2" point to the same file. Since both paths do not exist, the method returns "FileNotFoundError" −

import os
path1 = "/home/lenovo/documents/file.txt"
path2 = "/home/lenovo/documents/file.txt"
result = os.path.samefile(path1, path2)
print(result)  

Output

The result produced is as follows −

Traceback (most recent call last):
  File "C:\Users\Lenovo\Desktop\untitled.py", line 4, in <module>
    result = os.path.samefile(path1, path2)
  File "<frozen genericpath>", line 112, in samefile
FileNotFoundError: [WinError 3] The system cannot find the path specified: '/home/lenovo/documents/file.txt'
os_path_methods.htm
Advertisements