Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-105793: Add follow_symlinks argument topathlib.Path.is_dir() andis_file()#105794

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from1 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
PrevPrevious commit
NextNext commit
Also add argument tois_file().
  • Loading branch information
@barneygale
barneygale committedJun 15, 2023
commit1f666709768e4c2de75995ad5ab391143306d95c
18 changes: 12 additions & 6 deletionsDoc/library/pathlib.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -958,24 +958,30 @@ call fails (for example because the path doesn't exist).
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.

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

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


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

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

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

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

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


.. method:: Path.is_junction()

Expand Down
4 changes: 2 additions & 2 deletionsLib/pathlib.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -825,13 +825,13 @@ def is_dir(self, *, follow_symlinks=True):
# Non-encodable path
return False

def is_file(self):
def is_file(self, *, follow_symlinks=True):
"""
Whether this path is a regular file (also True for symlinks pointing
to regular files).
"""
try:
return S_ISREG(self.stat().st_mode)
return S_ISREG(self.stat(follow_symlinks=follow_symlinks).st_mode)
except OSError as e:
if not _ignore_error(e):
raise
Expand Down
17 changes: 15 additions & 2 deletionsLib/test/test_pathlib.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2205,8 +2205,21 @@ def test_is_file(self):
self.assertTrue((P / 'linkA').is_file())
self.assertFalse((P / 'linkB').is_file())
self.assertFalse((P/ 'brokenLink').is_file())
self.assertIs((P / 'fileA\udfff').is_file(), False)
self.assertIs((P / 'fileA\x00').is_file(), False)
self.assertFalse((P / 'fileA\udfff').is_file())
self.assertFalse((P / 'fileA\x00').is_file())

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

def test_is_mount(self):
P = self.cls(BASE)
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
Add *follow_symlinks* keyword-only argument to :meth:`pathlib.Path.is_dir`,
defaulting to ``True``.
Add *follow_symlinks* keyword-only argument to :meth:`pathlib.Path.is_dir` and
:meth:`~pathlib.Path.is_file`,defaulting to ``True``.

[8]ページ先頭

©2009-2025 Movatter.jp