<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">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><div>indeed, but not always, so a simple API that allows you to get a flat walk would be nice....</div><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 class=""><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><div>sure, but having to write "little" wrappers for common needs is unfortunate...</div><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 class=""><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><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><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 class=""><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><div>but they should -- of course they should..... </div><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><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><div><br></div></div>-- <br><div class="gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R Â  Â  Â  Â  Â  Â (206) 526-6959   voice<br>7600 Sand Point Way NE Â Â (206) 526-6329   fax<br>Seattle, WA Â 98115 Â  Â  Â Â (206) 526-6317   main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</div></div>