Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
GH-105793: Add follow_symlinks argument to pathlib.Path.is_dir()
Brings `pathlib.Path.is_dir()` in line with `os.DirEntry.is_dir()`, which
will be important for implementing generic path walking and globbing.
  • Loading branch information
barneygale committed Jun 14, 2023
commit 7c2c51adab68448ef1eba4118b669bbf9cbad483
12 changes: 9 additions & 3 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -953,14 +953,20 @@ call fails (for example because the path doesn't exist).
if the file's gid isn't found in the system database.


.. method:: Path.is_dir()
.. method:: Path.is_dir(*, follow_symlinks=True)

Return ``True`` if the path points to a directory (or a symbolic link
pointing to a directory), ``False`` if it points to another kind of file.
Return ``True`` if the path points to a directory, ``False`` if it points
to another kind of file.

This method normally follows symlinks; to exclude symlinks to directories,
add the argument ``follow_symlinks=False``.

``False`` is also returned if the path doesn't exist or is a broken symlink;
other errors (such as permission errors) are propagated.

.. versionchanged:: 3.13
The *follow_symlinks* parameter was added.


.. method:: Path.is_file()

Expand Down
4 changes: 2 additions & 2 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,12 +809,12 @@ def exists(self, *, follow_symlinks=True):
return False
return True

def is_dir(self):
def is_dir(self, *, follow_symlinks=True):
"""
Whether this path is a directory.
"""
try:
return S_ISDIR(self.stat().st_mode)
return S_ISDIR(self.stat(follow_symlinks=follow_symlinks).st_mode)
except OSError as e:
if not _ignore_error(e):
raise
Expand Down
19 changes: 16 additions & 3 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2623,9 +2623,22 @@ def test_is_dir(self):
if os_helper.can_symlink():
self.assertFalse((P / 'linkA').is_dir())
self.assertTrue((P / 'linkB').is_dir())
self.assertFalse((P/ 'brokenLink').is_dir(), False)
self.assertIs((P / 'dirA\udfff').is_dir(), False)
self.assertIs((P / 'dirA\x00').is_dir(), False)
self.assertFalse((P/ 'brokenLink').is_dir())
self.assertFalse((P / 'dirA\udfff').is_dir())
self.assertFalse((P / 'dirA\x00').is_dir())

def test_is_dir_no_follow_symlinks(self):
P = self.cls(BASE)
self.assertTrue((P / 'dirA').is_dir(follow_symlinks=False))
self.assertFalse((P / 'fileA').is_dir(follow_symlinks=False))
self.assertFalse((P / 'non-existing').is_dir(follow_symlinks=False))
self.assertFalse((P / 'fileA' / 'bah').is_dir(follow_symlinks=False))
if os_helper.can_symlink():
self.assertFalse((P / 'linkA').is_dir(follow_symlinks=False))
self.assertFalse((P / 'linkB').is_dir(follow_symlinks=False))
self.assertFalse((P/ 'brokenLink').is_dir(follow_symlinks=False))
self.assertFalse((P / 'dirA\udfff').is_dir(follow_symlinks=False))
self.assertFalse((P / 'dirA\x00').is_dir(follow_symlinks=False))

def test_is_file(self):
P = self.cls(BASE)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add *follow_symlinks* keyword-only argument to :meth:`pathlib.Path.is_dir`,
defaulting to ``True``.