This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: add pathlib.Path.walk method
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.11
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Ovsyanka, ajoino, barneygale, eric.araujo
Priority: normal Keywords: patch

Created on 2022-01-02 18:34 by Ovsyanka, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 30340 open Ovsyanka, 2022-01-02 21:14
Messages (4)
msg409511 - (view) Author: Stanislav Zmiev (Ovsyanka) * Date: 2022-01-02 18:34
Pathlib is great, yet every time I have to parse a bunch of files, I have to use os.walk and join paths by hand. That's not a lot of code but I feel like pathlib should have higher-level abstractions for all path-related functionality of os. I propose we add a Path.walk method that could look like this:

def walk(self, topdown=True, onerror=None, followlinks=False):
    for root, dirs, files in self._accessor.walk(
        root_path = Path(root)
        yield (
            [root_path._make_child_relpath(dir_) for dir_ in dirs],
            [root_path._make_child_relpath(file) for file in files],

Note: this version does not handle a situation when top does not exist (similar to os.walk that also doesn't handle it and just returns an empty generator)
msg409514 - (view) Author: Stanislav Zmiev (Ovsyanka) * Date: 2022-01-02 19:42
Some people could suggest using Path.glob instead but I found it to be less convenient for some use cases and generally slower (~2.7 times slower).

>>> timeit("list(Path('Lib').walk())", number=100, globals=globals())
>>> timeit("list(Path('Lib').glob('**/*'))", number=100, globals=globals())
msg409994 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2022-01-07 18:30
The idea is interesting, and I agree that glob with a maxi wildcard is not a great solution.  There is discussion on the PR about adding walk vs extending iterdir; could you post a message on and sum up the the discussion?  (Pull requests on the CPython repo are only used to discuss implementation, not for debating ideas or proposing features.)
msg410098 - (view) Author: Stanislav Zmiev (Ovsyanka) * Date: 2022-01-08 12:58
Thanks for the tip! Hopefully, I created it correctly:

It is currently on review.
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90385
2022-01-08 12:58:28Ovsyankasetmessages: + msg410098
2022-01-07 18:30:52eric.araujosetnosy: + eric.araujo
messages: + msg409994
2022-01-02 21:14:54Ovsyankasetkeywords: + patch
stage: patch review
pull_requests: + pull_request28552
2022-01-02 20:00:42ajoinosetnosy: + ajoino
2022-01-02 19:43:00Ovsyankasetmessages: + msg409514
2022-01-02 19:00:25barneygalesetnosy: + barneygale
2022-01-02 18:34:12Ovsyankacreate