[Python-ideas] find-like functionality in pathlib

Guido van Rossum guido at python.org
Wed Jan 6 15:00:24 EST 2016


On Mon, Jan 4, 2016 at 9:25 PM, Guido van Rossum <guido at python.org> wrote:

> Following up on this, in theory the right way to walk a tree using pathlib
> already exists, it's the rglob() method. E.g. all paths under /foo/bar
> should be found as follows:
>
>   for path in pathlib.Path('/foo/bar').rglob('**/*'):
>
[actually, rglob('*') or glob('**/*')]

>       print(path)
>
> The PermissionError bug you found is already reported:
> http://bugs.python.org/issue24120 -- it even has  a patch but it's stuck
> in review.
>

I committed this fix.


> Sadly there's another error: loops introduced by symlinks cause infinite
> recursion. I filed that here: http://bugs.python.org/issue26012. (The fix
> should be judicious use of is_symlink(), but the code is a little
> convoluted.)
>

I committed a fix for this too (turned out to need just one call to
is_symlink()).

I also added a .path attribute to pathlib.*Path objects, so that p.path ==
str(p). You can now use the idiom getattr(arg, 'path', arg) to extract the
path from a pathlib.Path object, or from an os.DirEntry object, or fall
back to a plain string, without using str(arg), which would turn *any*
object into a string, which is never what you want to happen by default.

These changes will be released in Python 3.4.5, 3.5.2 and 3.6.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160106/35e368de/attachment.html>


More information about the Python-ideas mailing list