Skip to content

gh-78318: Add pathlib.Path.lexists and related #21157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
17 changes: 15 additions & 2 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ 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::

Expand All @@ -833,7 +833,11 @@ call fails (for example because the path doesn't exist).

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

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


.. method:: Path.expanduser()
Expand Down Expand Up @@ -1097,6 +1101,14 @@ call fails (for example because the path doesn't exist).
symbolic link's mode is changed rather than its target's.


.. method:: Path.lexists()

Like :meth:`Path.exists` but, if the path points to a symbolic link, return
the symbolic link's information rather than its target's.

.. versionadded:: 3.10


.. method:: Path.lstat()

Like :meth:`Path.stat` but, if the path points to a symbolic link, return
Expand Down Expand Up @@ -1436,6 +1448,7 @@ Below is a table mapping various :mod:`os` functions to their corresponding
:func:`os.path.isdir` :meth:`Path.is_dir`
:func:`os.path.isfile` :meth:`Path.is_file`
:func:`os.path.islink` :meth:`Path.is_symlink`
:func:`os.path.lexists` :meth:`Path.lexists`
:func:`os.link` :meth:`Path.hardlink_to`
:func:`os.symlink` :meth:`Path.symlink_to`
:func:`os.readlink` :meth:`Path.readlink`
Expand Down
18 changes: 16 additions & 2 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,12 +1061,18 @@ def hardlink_to(self, target):

# Convenience functions for querying the stat results

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

Returns False for broken symbolic links unless follow_symlinks is set
to False.
"""
try:
self.stat()
if follow_symlinks:
self.stat()
else:
self.lstat()
except OSError as e:
if not _ignore_error(e):
raise
Expand Down Expand Up @@ -1200,6 +1206,14 @@ def is_socket(self):
# Non-encodable path
return False

def lexists(self):
"""
Whether this path exists, but don't follow symbolic links.

Returns True for broken symbolic links.
"""
return self.exists(follow_symlinks=False)

def expanduser(self):
""" Return a new path with expanded ~ and ~user constructs
(as returned by os.path.expanduser)
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1646,11 +1646,31 @@ 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(False, (p / 'brokenLinkLoop').exists())
self.assertIs(True, (p / 'brokenLink').exists(
follow_symlinks=False))
self.assertIs(True, (p / 'brokenLinkLoop').exists(
follow_symlinks=False))
self.assertIs(False, (p / 'foo').exists())
self.assertIs(False, P('/xyzzy').exists())
self.assertIs(False, P(BASE + '\udfff').exists())
self.assertIs(False, P(BASE + '\x00').exists())

@os_helper.skip_unless_symlink
def test_exists_follow_symlinks(self):
P = self.cls
p = P(BASE)
self.assertIs(True, (p / 'brokenLink').exists(follow_symlinks=False))
self.assertIs(True, (p / 'brokenLinkLoop').exists(follow_symlinks=False))

@os_helper.skip_unless_symlink
def test_lexists(self):
P = self.cls
p = P(BASE)
self.assertIs(True, (p / 'brokenLink').lexists())
self.assertIs(True, (p / 'brokenLinkLoop').lexists())

def test_open_common(self):
p = self.cls(BASE)
with (p / 'fileA').open('r') as f:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Add the *follow_symlinks* parameter in :meth:`pathlib.Path.exists` and
:meth:`pathlib.Path.lexists` as a wrapper analogous to
:func:`os.stat` ./. :func:`os.lstat` and
:func:`os.path.exists` ./. :func:`os.path.lexists`.