Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
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
15 changes: 9 additions & 6 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,14 @@ call fails (for example because the path doesn't exist).
.. versionchanged:: 3.10
The *follow_symlinks* parameter was added.

.. method:: Path.exists()
.. method:: Path.exists(follow_symlinks=True)

Whether the path points to an existing file or directory::
Return ``True`` if the path points to an existing file or directory.

This method normally follows symlinks; to check if a symlink exists, add
the argument ``follow_symlinks=False``.

::

>>> Path('.').exists()
True
Expand All @@ -768,10 +773,8 @@ call fails (for example because the path doesn't exist).
>>> Path('nonexistentfile').exists()
False

.. note::
If the path points to a symlink, :meth:`exists` returns whether the
symlink *points to* an existing file or directory.

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

.. method:: Path.expanduser()

Expand Down
11 changes: 8 additions & 3 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ def __init__(self, name, child_parts, flavour):
def _select_from(self, parent_path, is_dir, exists, scandir):
try:
path = parent_path._make_child_relpath(self.name)
if (is_dir if self.dironly else exists)(path):
if ((self.dironly and is_dir(path)) or \
(not self.dironly and exists(path, follow_symlinks=False))):
for p in self.successor._select_from(path, is_dir, exists, scandir):
yield p
except PermissionError:
Expand Down Expand Up @@ -1280,12 +1281,16 @@ def link_to(self, target):

# Convenience functions for querying the stat results

def exists(self):
def exists(self, follow_symlinks=True):
"""
Whether this path exists.

If path is a symlink: with `follow_symlinks=True`, return True if path
pointed to by the symlink exists; with follow_symlinks=False, return
True if the symlink itself exists.
"""
try:
self.stat()
self.stat(follow_symlinks=follow_symlinks)
except OSError as e:
if not _ignore_error(e):
raise
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,8 @@ def test_exists(self):
self.assertIs(True, (p / 'linkB').exists())
self.assertIs(True, (p / 'linkB' / 'fileB').exists())
self.assertIs(False, (p / 'linkA' / 'bah').exists())
self.assertIs(False, (p / 'brokenLink').exists())
self.assertIs(True, (p / 'brokenLink').exists(follow_symlinks=False))
self.assertIs(False, (p / 'foo').exists())
self.assertIs(False, P('/xyzzy').exists())
self.assertIs(False, P(BASE + '\udfff').exists())
Expand Down Expand Up @@ -1631,6 +1633,8 @@ def _check(glob, expected):
_check(p.glob("*/fileB"), ['dirB/fileB'])
else:
_check(p.glob("*/fileB"), ['dirB/fileB', 'linkB/fileB'])
if os_helper.can_symlink():
_check(p.glob("brokenLink"), ['brokenLink'])

def test_rglob_common(self):
def _check(glob, expected):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fixed the bug in :meth:`~pathlib.Path.glob` -- previously a dangling symlink
would not be found by this method when the pattern is an exact match, but
would be found when the pattern contains a wildcard or the recursive
wildcard (``**``). With this change, a dangling symlink will be found in
both cases.