Skip to content

Conversation

@LiuQhahah
Copy link

@LiuQhahah LiuQhahah commented Nov 4, 2025

Fixes #140971
This PR clarifies the behavior of pathlib.Path("") in the documentation.

The current behavior, where pathlib.Path("").exists() returns True, is intentional but
can be confusing because it differs from os.path.exists(""), which returns False.

This change adds a note to the PurePath constructor documentation to explain that an empty string path segment refers to the current directory. This makes the behavior explicit to users and helps prevent further confusion.


📚 Documentation preview 📚: https://cpython-previews--140993.org.readthedocs.build/

@python-cla-bot
Copy link

python-cla-bot bot commented Nov 4, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

Comment on lines +139 to +145
An empty string in *pathsegments* also refers to the current directory.
This means that ``PurePath("")`` is equivalent to ``PurePath(".")``,
and consequently ``Path("").exists()`` will always return ``True``. This
differs from ``os.path.exists("")``, which returns ``False``.

>>> PurePath("")
PurePosixPath('.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the new text should be folded into the previous 4 lines. The result would then be:

When pathsegments is empty or a single empty string, the current directory is assumed::

  >>> PurePath(), PurePath('')
  (PurePosixPath('.'), PurePosixPath('.'))

The boolean value of either expression is True.
This differs from os.path.exists(""), which returns False.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

unexpected behavior of pathlib with empty string

2 participants