-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
add pathlib.Path.walk method #90385
Comments
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(
self,
topdown=topdown,
onerror=onerror,
followlinks=followlinks
):
root_path = Path(root)
yield (
root_path,
[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) |
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())
1.9074640140170231
>>> timeit("list(Path('Lib').glob('**/*'))", number=100, globals=globals())
5.14890358998673 |
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.) |
Thanks for the tip! Hopefully, I created it correctly: It is currently on review. |
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. |
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. |
GH-92517 broke tests on wasm32-wasi platform.
|
@tiran I apologize for introducing this problem and I am really thankful to you for resolving it, especially so quickly! |
No worry! We all forgot to run the PR through buildbots before merging, so let’s just do that next time. |
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. |
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 |
@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. |
…thub.com:Ovsyanka83/cpython into pythongh-90385/add-pathlib-path-whatsnew-section
Automerge-Triggered-By: GH:brettcannon
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: