Python os.path.samefile() Method

Python os.path.samefile() Method

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

Python os.path.samefile() method is an efficient way to check if the file path entered refers to the same directory or not. The working mechanism behind this method is to compare the i-node number and device number of the given paths.

Syntax of Python os.path.samefile() Method

os.path.samefile(path1, path2)

Parameters

path1 It is an address object of the first file system path or a symlink. The object can either be an str or bytes.
path2 It is an address object of the second file system path or a symlink. The object can either be an str or bytes.

Return

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

Example 1: Use os.path.samefile() Method to Check if Paths Refer to the Same File or Directory

import os

path1 = "C:/Users/786/Documents/File.txt"

link = "C:/Users/786/Desktop/File(shortcut).txt"

os.symlink(path1, link)

similarity = os.path.samefile(path1, link)

print("Are the paths pointing to the same file? ", similarity)

Output:

Are the paths pointing to the same file?  True

In the background, this method uses the os.stat() method to get device numbers and i-node numbers of the specified paths.

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

import os

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

path1 ="/home/File.txt"

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

similarity = os.path.samefile(path1, path2)

print("Are the paths pointing to the same file? ", similarity)

Output:

Are the paths pointing to the same file?  True

Note that you might have written the correct code for the os.path.samefile() method, and still, an exception is thrown. This might be because the os.stat() call fails on either file pathnames.

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

import os

def os_path_samefile(path1, path2):

  stat1 = os.stat(path1) if os.path.isfile(path1) else None
  if not stat1:
    return False
  stat2 = os.stat(path2) if os.path.isfile(path2) 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 )

path1 ="/home/File.txt"

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

similarity = os_path_samefile(path1, path2)

print("Are the paths pointing to the same file? ", similarity)

Output:

Are the paths pointing to the same file?  True

In some versions of Python, the original os.path.samefile() 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