[Python-ideas] Please consider skipping hidden directories in os.walk, os.fwalk, etc.

Giampaolo Rodola' g.rodola at gmail.com
Tue May 8 10:53:26 EDT 2018


On Tue, May 8, 2018 at 2:00 PM, David Mertz <mertz at gnosis.cx> wrote:

> I like the idea. I think an argument to os.walk() is the simplest option
> for most users. But per some comments, "hidden" is actually more subtle
> than the filesystem bit sometimes. I.e. dot-files, ~ suffix, maybe .bak,
> etc.
>
> I'd suggest meeting the ideas slightly and making the new argument
> 'filter' or 'skip' that takes a callable. Default to None, but provide an
> os.is_hidden that users don't need to figure out how to implement. E.g.
>
> os.walk(PATH, skip=os.is_hidden)
>
> os.walk(PATH, skip=lambda entry: entry.name.endswith(('~', '.bak',
> '.tmp')))
>

I think this would be a good addition because it gives direct access to the
underlying os.scandir() objects which are currently inaccessible and
discarded (if os.walk() were to be written today it'd probably yield (root,
os.DirEntry) instead of (root, dirs, files)). As such one can implement
advanced filtering logic without having to call os.stat() for each path
string yielded by os.walk() (faster).

IMO the callback should accept a (root, os.DirEntry) pair though, because the
"root" path can also be part of the filtering logic.

-- 

Giampaolo - http://grodola.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180508/ea5de885/attachment.html>


More information about the Python-ideas mailing list