<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Mon, May 7, 2018 at 9:44 PM Steve Barnes <<a href="mailto:gadgetsteve@live.co.uk">gadgetsteve@live.co.uk</a>> wrote:</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Since the implementation of os.walk has changed to use os.scandir which <br>
exposes the returned file statuses in the os.DirEntry.stat() the <br>
overhead should be minimal.<br>
<br>
An alternative would be to add another new function, say os.vwalk(), to <br>
only walk visible entries.<br></blockquote><div><br></div><div>
<div dir="ltr" style="color:rgb(34,34,34);font-family:sans-serif;font-size:13px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">On Tue, May 8, 2018 at 12:06 AM Steven D'Aprano <<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> wrote:<br></div><blockquote class="gmail_quote" style="color:rgb(34,34,34);font-family:sans-serif;font-size:13px;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I would write something like:<br> for root, dirs, files in filter(ignorable, os.walk(some_dir)):</blockquote>
<br></div><div>I agree with Steven with regards to `filter` needing to be flexible. If you want to avoid duplicate `stat` calls, you'll probably write:</div><div><br></div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_quote"><div><div>import os</div></div></div><div class="gmail_quote"><div><div>import stat</div></div></div><div class="gmail_quote"><div><div>def is_hidden(st):</div></div></div><div class="gmail_quote"><div><div> return bool(st.st_file_attributes & stat.FILE_ATTRIBUTE_HIDDEN)</div></div></div><div class="gmail_quote"><div><div>def visible_walk(path):</div></div></div><div class="gmail_quote"><div><div> for entry in os.scandir(path):</div></div></div><div class="gmail_quote"><div><div> if entry.is_dir():</div></div></div><div class="gmail_quote"><div><div> if not is_hidden(entry.stat()):</div></div></div><div class="gmail_quote"><div><div> yield from visible_walk(entry.path)</div></div></div><div class="gmail_quote"><div><div> else:</div></div></div><div class="gmail_quote"><div><div> if not is_hidden(entry.stat()):</div></div></div><div class="gmail_quote"><div><div> yield entry.path</div></div></div></blockquote><div class="gmail_quote"><div><br></div><div>Then you can decide whether you want to ignore hidden files or just hidden directories. The variations for such a need are many. So it makes sense to leave any specific filtering need outside of the standard library. A PyPI package with a few standard filtered walks could be a nice exploration for this idea.</div><div><br></div><div>Cheers,</div><div><br></div><div>Yuval</div></div></div>