Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 43e3d94

Browse files
committed
Issue #19775: Add a samefile() method to pathlib Path objects.
Initial patch by Vajrasky Kok.
1 parent 38acd4c commit 43e3d94

4 files changed

Lines changed: 53 additions & 0 deletions

File tree

Doc/library/pathlib.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,25 @@ call fails (for example because the path doesn't exist):
884884
Remove this directory. The directory must be empty.
885885

886886

887+
.. method:: Path.samefile(other_path)
888+
889+
Return whether this path points to the same file as *other_path*, which
890+
can be either a Path object, or a string. The semantics are similar
891+
to :func:`os.path.samefile` and :func:`os.path.samestat`.
892+
893+
An :exc:`OSError` can be raised if either file cannot be accessed for some
894+
reason.
895+
896+
>>> p = Path('spam')
897+
>>> q = Path('eggs')
898+
>>> p.samefile(q)
899+
False
900+
>>> p.samefile('spam')
901+
True
902+
903+
.. versionadded:: 3.5
904+
905+
887906
.. method:: Path.symlink_to(target, target_is_directory=False)
888907

889908
Make this path a symbolic link to *target*. Under Windows,

Lib/pathlib.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,17 @@ def cwd(cls):
961961
"""
962962
return cls(os.getcwd())
963963

964+
def samefile(self, other_path):
965+
"""Return whether `other_file` is the same or not as this file.
966+
(as returned by os.path.samefile(file, other_file)).
967+
"""
968+
st = self.stat()
969+
try:
970+
other_st = other_path.stat()
971+
except AttributeError:
972+
other_st = os.stat(other_path)
973+
return os.path.samestat(st, other_st)
974+
964975
def iterdir(self):
965976
"""Iterate over the files in this directory. Does not yield any
966977
result for the special paths '.' and '..'.

Lib/test/test_pathlib.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,26 @@ def test_cwd(self):
12511251
p = self.cls.cwd()
12521252
self._test_cwd(p)
12531253

1254+
def test_samefile(self):
1255+
fileA_path = os.path.join(BASE, 'fileA')
1256+
fileB_path = os.path.join(BASE, 'dirB', 'fileB')
1257+
p = self.cls(fileA_path)
1258+
pp = self.cls(fileA_path)
1259+
q = self.cls(fileB_path)
1260+
self.assertTrue(p.samefile(fileA_path))
1261+
self.assertTrue(p.samefile(pp))
1262+
self.assertFalse(p.samefile(fileB_path))
1263+
self.assertFalse(p.samefile(q))
1264+
# Test the non-existent file case
1265+
non_existent = os.path.join(BASE, 'foo')
1266+
r = self.cls(non_existent)
1267+
self.assertRaises(FileNotFoundError, p.samefile, r)
1268+
self.assertRaises(FileNotFoundError, p.samefile, non_existent)
1269+
self.assertRaises(FileNotFoundError, r.samefile, p)
1270+
self.assertRaises(FileNotFoundError, r.samefile, non_existent)
1271+
self.assertRaises(FileNotFoundError, r.samefile, r)
1272+
self.assertRaises(FileNotFoundError, r.samefile, non_existent)
1273+
12541274
def test_empty_path(self):
12551275
# The empty path points to '.'
12561276
p = self.cls('')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ Core and Builtins
8484
Library
8585
-------
8686

87+
- Issue #19775: Add a samefile() method to pathlib Path objects. Initial
88+
patch by Vajrasky Kok.
89+
8790
- Issue #21398: Fix an unicode error in the pydoc pager when the documentation
8891
contains characters not encodable to the stdout encoding.
8992

0 commit comments

Comments
 (0)