<div dir="ltr"><div><div><div>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:<br><br></div>  for path in pathlib.Path('/foo/bar').rglob('**/*'):<br>      print(path)<br><br></div>The PermissionError bug you found is already reported: <a href="http://bugs.python.org/issue24120">http://bugs.python.org/issue24120</a> -- it even has  a patch but it's stuck in review.<br><br></div>Sadly there's another error: loops introduced by symlinks cause infinite recursion. I filed that here: <a href="http://bugs.python.org/issue26012">http://bugs.python.org/issue26012</a>. (The fix should be judicious use of is_symlink(), but the code is a little convoluted.)<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Dec 28, 2015 at 11:25 AM, Chris Barker <span dir="ltr"><<a href="mailto:chris.barker@noaa.gov" target="_blank">chris.barker@noaa.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="">On Tue, Dec 22, 2015 at 4:23 PM, Guido van Rossum <span dir="ltr"><<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">The two-level iteration forced upon you by os.walk() is indeed often unnecessary -- but handling dirs and files separately usually makes sense, </div></div></div></blockquote><div><br></div></span><div>indeed, but not always, so a simple API that allows you to get a flat walk would be nice....</div><span class=""><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Of course for that basic use case, you could just write your own wrapper around os.walk:<br></blockquote></span></div></div></div></blockquote><div><br></div></span><div>sure, but having to write "little" wrappers for common needs is unfortunate...</div><span class=""><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">The problem isn't designing a nice walk API; it's integrating it with pathlib.* </blockquote></span></div></div></div></blockquote><div><br></div></span><div>indeed -- I'd really like to see a *walk in pathlib itself. I've been trying to use pathlib whenever I need, well, a path, but then I find I almost immediately need to step out and use an os.path function, and have to string-fy it anyway -- makes me wonder what the point is..</div><span class=""><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> And honestly, if open, os.walk, etc. aren't going to work with Path objects, </blockquote></span></div></div></div></blockquote><div><br></div></span><div>but they should -- of course they should..... </div><span class=""><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Truly pushing for adoption of a new abstraction like this takes many years -- pathlib was new (and provisional) in 3.4 so it really hasn't been long enough to give up on it. The OP hasn't!</div></div></div></div></blockquote><div><br></div></span><div>it will take many years for sure -- but the standard library cold at least adopt it as much as possible.</div><div><br></div><div>Path.walk would be a nice start :-)</div><div><br></div><div>My example: one of our sysadmins wanted a little script to go thorugh an entire drive (Windows), and check if any paths were longer than 256 characters (Windows, remember..)</div><div><br></div><div>I came up with this:</div><div><br></div><div><div>def get_all_paths(start_dir='/'):</div><div>    for dirpath, dirnames, filenames in os.walk(start_dir):</div><div>        for filename in filenames:</div><div>            yield os.path.join(dirpath, filename)</div><div><br></div><div>too_long = []</div><div>for p in get_all_paths('/'):</div><div>    print("checking:", p)</div><div>    if len(p) > 255:</div><div>        too_long.append(p)</div><div>        print("Path too long!")</div></div><div><br></div><div>way too wordy! </div><div><br></div><div>I started with pathlib, but that just made it worse.</div><div><br></div><div>now that I think about it, maybe I could have simpily used pathlib.Path.rglob....</div><div><br></div><div>However, when I try that, I get a permission error:</div><div><br></div>/Users/chris.barker/miniconda2/envs/py3/lib/python3.5/pathlib.py in wrapped(pathobj, *args)<br><br>    369         @functools.wraps(strfunc)<br>    370         def wrapped(pathobj, *args):<br>--> 371             return strfunc(str(pathobj), *args)<br>    372         return staticmethod(wrapped)<br>    373 <br><br>PermissionError: [Errno 13] Permission denied: '/Users/.chris.barker.xahome/caches/opendirectory'<div><br></div><div>as the error comes insider the rglob() generator, I'm not sure how to tell it to ignore and move on....</div><div><br></div><div>os.walk is somehow able to deal with this.</div><div><br></div><div>-CHB</div><span class="HOEnZb"><font color="#888888"><div><br></div></font></span></div><span class="HOEnZb"><font color="#888888">-- <br><div><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R            <a href="tel:%28206%29%20526-6959" value="+12065266959" target="_blank">(206) 526-6959</a>   voice<br>7600 Sand Point Way NE   <a href="tel:%28206%29%20526-6329" value="+12065266329" target="_blank">(206) 526-6329</a>   fax<br>Seattle, WA  98115       <a href="tel:%28206%29%20526-6317" value="+12065266317" target="_blank">(206) 526-6317</a>   main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</font></span></div></div>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>