You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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)
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 discuss.python.org 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.)
The original solution was based on wrapping os.walk but we have decided that it will be both faster and better in the long term to reimplement it (though I was not able to come up with any implementation that is significantly better than that of os.walk so it's 99% the same)
The original solution was yielding a tuple[Path, list[Path], list[Path]] but we decided to stay true to the original implementation and yield tuple[Path, list[str], list[str]]. It is both better in terms of optimization and should cause a little less confusion to the users
The original solution had the same arguments as os.walk but the interface was too convoluted so: we decided to rename arguments to snake-case and I decided to remove topdown argument by splitting walk into two methods. So we get walk() and walk_bottom_up(). I.e. Now we get two methods instead of one but with two modes of operation. The naming of methods is up to discussion because I myself am not very happy with these names but those are the best I found
I have been absent for quite some time so the pull request got stale and outdated so I'm opening a new one.
@Ovsyanka83 it should end up in What's New. If you would like to write a PR for that I will happily review it. We also go through the changelog before release to make sure everything relevant ends up in there, so we don't have to worry about it being missed.
Activity
Ovsyanka83 commentedon Jan 2, 2022
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:
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)
Ovsyanka83 commentedon Jan 2, 2022
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).
merwok commentedon Jan 7, 2022
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 discuss.python.org 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.)
Ovsyanka83 commentedon Jan 8, 2022
Thanks for the tip! Hopefully, I created it correctly:
https://discuss.python.org/t/add-pathlib-path-walk-method/
It is currently on review.
zmievsa commentedon May 8, 2022
The discussion ended on the following decisions:
I have been absent for quite some time so the pull request got stale and outdated so I'm opening a new one.
brettcannon commentedon Jul 8, 2022
The views on the change seem generally positive and the API seems like it makes sense, so I'm inclined to accept the change once the PR passes review.
4 remaining items
pythongh-90385: Add skip_unless_symlink decorator to test_walk_symlin…
gh-90385: Add skip_unless_symlink decorator to test_walk_symlink_loca…
zmievsa commentedon Jul 24, 2022
@tiran I apologize for introducing this problem and I am really thankful to you for resolving it, especially so quickly!
merwok commentedon Jul 24, 2022
No worry! We all forgot to run the PR through buildbots before merging, so let’s just do that next time.
brettcannon commentedon Jul 25, 2022
It's also partially my fault by forgetting that WASI doesn't support symlinks and thus to have you add the test guard for those sorts of platforms.
zmievsa commentedon Jul 26, 2022
By the way, are we adding this PR into what's new? Or is it only gonna be a part of changelog? Seems big enough for what's new
brettcannon commentedon Jul 26, 2022
@Ovsyanka83 it should end up in What's New. If you would like to write a PR for that I will happily review it. We also go through the changelog before release to make sure everything relevant ends up in there, so we don't have to worry about it being missed.
Merge branch 'main' into pythongh-90385/add-pathlib-path-whatsnew-sec…
Merge branch 'pythongh-90385/add-pathlib-path-whatsnew-section' of gi…
Merge branch 'main' into pythongh-90385/add-pathlib-path-whatsnew-sec…
gh-90385: Add pathlib.Path.walk what's new section (GH-95467)