all Python 3osfunction
os.path.samefile
os.path — metadata (stat)
Check whether two paths refer to the same file (by device and inode).
Syntax
os.path.samefile(p1, p2)
It recognises one file under different names: hard links, symlinks, paths with '..' and different letter case. That is more reliable than comparing path strings. Both paths must exist, or FileNotFoundError is raised. It helps, for instance, to avoid copying a file onto itself.
Examples
import os
print(os.path.samefile(os.__file__, os.__file__)) # → True
same = os.path.join(os.path.dirname(os.__file__), '.', os.path.basename(os.__file__))
print(os.path.samefile(os.__file__, same)) # → True
print(os.__file__ == same) # → False (строки разные, а файл один и тот же)
print(os.path.samefile(os.__file__, os.path.dirname(os.__file__))) # → False (файл и его каталог — разные объекты ФС)
print(os.path.samefile('нет-такого', os.__file__)) # → FileNotFoundError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy