-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
GH-139174: Add pathlib.Path.info.stat()
#139279
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
53b28f4
e335864
02cfee1
1fa73e6
cb3efe7
1d3b874
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ in this chapter is: | |
| .. toctree:: | ||
|
|
||
| pathlib.rst | ||
| pathlib.types.rst | ||
| os.path.rst | ||
| stat.rst | ||
| filecmp.rst | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1173,7 +1173,7 @@ Querying file type and status | |
|
|
||
| .. attribute:: Path.info | ||
|
|
||
| A :class:`~pathlib.types.PathInfo` object that supports querying file type | ||
| An :class:`Info` object that supports querying file type | ||
| information. The object exposes methods that cache their results, which can | ||
| help reduce the number of system calls needed when switching on file type. | ||
| For example:: | ||
|
|
@@ -1202,6 +1202,10 @@ Querying file type and status | |
|
|
||
| .. versionadded:: 3.14 | ||
|
|
||
| .. versionchanged:: next | ||
| Value is specifically a :class:`Info` object, rather than an unnamed | ||
| implementation of the :class:`pathlib.types.PathInfo` protocol. | ||
|
|
||
|
|
||
| Reading and writing files | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
@@ -1750,6 +1754,69 @@ Permissions and ownership | |
| symbolic link's mode is changed rather than its target's. | ||
|
|
||
|
|
||
| Path information | ||
| ---------------- | ||
|
|
||
| .. class:: Info | ||
|
|
||
| Class that supports fetching and caching information about a local | ||
| filesystem path. An instance of this class is available from | ||
| :attr:`Path.info`; do not instantiate it directly. | ||
|
|
||
| This is an implementation of the :class:`pathlib.types.PathInfo` protocol | ||
| with an additional method specific to local paths (:meth:`~Info.stat`.) | ||
|
|
||
| .. versionadded:: next | ||
|
|
||
| .. method:: exists(*, follow_symlinks=True) | ||
|
|
||
| Return ``True`` if the path is an existing file or directory, or any | ||
| other kind of file; return ``False`` if the path doesn't exist. | ||
|
|
||
| If *follow_symlinks* is ``False``, return ``True`` for symlinks without | ||
| checking if their targets exist. | ||
|
|
||
| .. method:: is_dir(*, follow_symlinks=True) | ||
|
|
||
| Return ``True`` if the path is a directory, or a symbolic link pointing | ||
| to a directory; return ``False`` if the path is (or points to) any other | ||
| kind of file, or if it doesn't exist. | ||
|
|
||
| If *follow_symlinks* is ``False``, return ``True`` only if the path | ||
| is a directory (without following symlinks); return ``False`` if the | ||
| path is any other kind of file, or if it doesn't exist. | ||
|
|
||
| .. method:: is_file(*, follow_symlinks=True) | ||
|
|
||
| Return ``True`` if the path is a file, or a symbolic link pointing to | ||
| a file; return ``False`` if the path is (or points to) a directory or | ||
| other non-file, or if it doesn't exist. | ||
|
|
||
| If *follow_symlinks* is ``False``, return ``True`` only if the path | ||
| is a file (without following symlinks); return ``False`` if the path | ||
| is a directory or other non-file, or if it doesn't exist. | ||
|
|
||
| .. method:: is_symlink() | ||
|
|
||
| Return ``True`` if the path is a symbolic link (even if broken); return | ||
| ``False`` if the path is a directory or any kind of file, or if it | ||
| doesn't exist. | ||
|
|
||
| .. method:: stat(*, follow_symlinks=True) | ||
|
|
||
| Return an :class:`os.stat_result` object containing low-level | ||
| information about this path, like :func:`os.stat`. The result is cached | ||
| on the :class:`Info` object. | ||
|
|
||
| If *follow_symlinks* is ``False`` and this path is a symlink, return | ||
| information about the symlink rather than its target. | ||
|
|
||
| On Windows, if the path was generated by scanning a directory, then the | ||
| ``st_ino``, ``st_dev`` and ``st_nlink`` attributes of the | ||
| :class:`~os.stat_result` are always set to zero. Call :meth:`Path.stat` | ||
| to get these attributes. | ||
|
Comment on lines
+1788
to
+1791
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, would it be better to call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When we implement #140127, we'll be able to recommend
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even there it would be great if IMO,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, will do. This will require merging the |
||
|
|
||
|
|
||
| .. _pathlib-pattern-language: | ||
|
|
||
| Pattern language | ||
|
|
@@ -1925,56 +1992,3 @@ Below is a table mapping various :mod:`os` functions to their corresponding | |
| .. [4] :func:`os.walk` always follows symlinks when categorizing paths into | ||
| *dirnames* and *filenames*, whereas :meth:`Path.walk` categorizes all | ||
| symlinks into *filenames* when *follow_symlinks* is false (the default.) | ||
|
|
||
|
|
||
| Protocols | ||
| --------- | ||
|
|
||
| .. module:: pathlib.types | ||
| :synopsis: pathlib types for static type checking | ||
|
|
||
|
|
||
| The :mod:`pathlib.types` module provides types for static type checking. | ||
|
|
||
| .. versionadded:: 3.14 | ||
|
|
||
|
|
||
| .. class:: PathInfo() | ||
|
|
||
| A :class:`typing.Protocol` describing the | ||
| :attr:`Path.info <pathlib.Path.info>` attribute. Implementations may | ||
| return cached results from their methods. | ||
|
|
||
| .. method:: exists(*, follow_symlinks=True) | ||
|
|
||
| Return ``True`` if the path is an existing file or directory, or any | ||
| other kind of file; return ``False`` if the path doesn't exist. | ||
|
|
||
| If *follow_symlinks* is ``False``, return ``True`` for symlinks without | ||
| checking if their targets exist. | ||
|
|
||
| .. method:: is_dir(*, follow_symlinks=True) | ||
|
|
||
| Return ``True`` if the path is a directory, or a symbolic link pointing | ||
| to a directory; return ``False`` if the path is (or points to) any other | ||
| kind of file, or if it doesn't exist. | ||
|
|
||
| If *follow_symlinks* is ``False``, return ``True`` only if the path | ||
| is a directory (without following symlinks); return ``False`` if the | ||
| path is any other kind of file, or if it doesn't exist. | ||
|
|
||
| .. method:: is_file(*, follow_symlinks=True) | ||
|
|
||
| Return ``True`` if the path is a file, or a symbolic link pointing to | ||
| a file; return ``False`` if the path is (or points to) a directory or | ||
| other non-file, or if it doesn't exist. | ||
|
|
||
| If *follow_symlinks* is ``False``, return ``True`` only if the path | ||
| is a file (without following symlinks); return ``False`` if the path | ||
| is a directory or other non-file, or if it doesn't exist. | ||
|
|
||
| .. method:: is_symlink() | ||
|
|
||
| Return ``True`` if the path is a symbolic link (even if broken); return | ||
| ``False`` if the path is a directory or any kind of file, or if it | ||
| doesn't exist. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| :mod:`!pathlib.types` --- Types for virtual filesystem paths | ||
| ============================================================ | ||
|
|
||
| .. module:: pathlib.types | ||
| :synopsis: pathlib types for virtual filesystem paths | ||
|
|
||
| .. versionadded:: 3.14 | ||
|
|
||
| **Source code:** :source:`Lib/pathlib/types.py` | ||
|
|
||
| -------------- | ||
|
|
||
| The :mod:`pathlib.types` module provides protocols for user-defined types that | ||
| resemble :class:`pathlib.Path` and its associated classes. This module | ||
| includes type annotations, so it's also useful for static type checking. | ||
|
|
||
| .. versionadded:: 3.14 | ||
|
|
||
|
|
||
| .. class:: PathInfo() | ||
|
|
||
| A :class:`typing.Protocol` that supports fetching and caching information | ||
| about a path. :class:`pathlib.Info` is an implementation of this protocol | ||
| with additional methods specific to local filesystems. | ||
|
|
||
| .. method:: exists(*, follow_symlinks=True) | ||
|
|
||
| Return ``True`` if the path is an existing file or directory, or any | ||
| other kind of file; return ``False`` if the path doesn't exist. | ||
|
|
||
| If *follow_symlinks* is ``False``, return ``True`` for symlinks without | ||
| checking if their targets exist. | ||
|
|
||
| .. method:: is_dir(*, follow_symlinks=True) | ||
|
|
||
| Return ``True`` if the path is a directory, or a symbolic link pointing | ||
| to a directory; return ``False`` if the path is (or points to) any other | ||
| kind of file, or if it doesn't exist. | ||
|
|
||
| If *follow_symlinks* is ``False``, return ``True`` only if the path | ||
| is a directory (without following symlinks); return ``False`` if the | ||
| path is any other kind of file, or if it doesn't exist. | ||
|
|
||
| .. method:: is_file(*, follow_symlinks=True) | ||
|
|
||
| Return ``True`` if the path is a file, or a symbolic link pointing to | ||
| a file; return ``False`` if the path is (or points to) a directory or | ||
| other non-file, or if it doesn't exist. | ||
|
|
||
| If *follow_symlinks* is ``False``, return ``True`` only if the path | ||
| is a file (without following symlinks); return ``False`` if the path | ||
| is a directory or other non-file, or if it doesn't exist. | ||
|
|
||
| .. method:: is_symlink() | ||
|
|
||
| Return ``True`` if the path is a symbolic link (even if broken); return | ||
| ``False`` if the path is a directory or any kind of file, or if it | ||
| doesn't exist. |
Uh oh!
There was an error while loading. Please reload this page.