Change language

Python | os.path.samefile () method

|

os.path.samefile() in Python is used to check if the specified two paths refer to the same file or directory or not. This is determined by comparing the device number and the i-node number of the given paths. 
This method uses os.stat () exception if any os.stat () call os.stat () will fail for any of the paths.

Syntax: os.path.samefile (path1, path2)

Parameter:
path1 : A path-like object representing the first file system path.
path2 : A path-like object representing the second file system path.

A path-like object is either a string or bytes object representing a path.

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

Code: Using the os.path.samefile () method to check if specified paths to the same file or directory.

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

 
# import of the os module

import os

 
# Path

path1 = "/ home / ihritik / Documents / file (original) .txt "

  
# Create a symbolic link

 sym_link = "/ home / ihritik / Desktop / file (shortcut) .txt"

os.symlink (path1, sym_link)

 
# Check if the given one matches
# paths link to the same
# file or directory or not

areSame = os.path.samefile (path1, sym_link)

 
# Print result

print (areSame)

 
# In the above example, sym_link
# symlink that links
# to path1, poet the os.path.samefile () method
# will return True, since both refer to
# to the same file

 

 
# First path

path2 = "/ home / ihritik / GeeksForGeeks"

 
# Second path
# view the current working directory
# is & quot; / home / ihritik & quot ;

path3 = os.path. join (os.getcwd (), "GeeksForGeeks" )

 
# Check if the given matches
  # paths refer to the same
# file or directory or not

areSame = os.path.samefile (path2, path3)

 
# Print result

print (areSame)

Output:

 True True 

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

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically