[Python-ideas] Please consider skipping hidden directories in os.walk, os.fwalk, etc.
David Mertz
mertz at gnosis.cx
Tue May 8 08:00:51 EDT 2018
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')))
On Tue, May 8, 2018, 5:47 AM Oleg Broytman <phd at phdru.name> wrote:
> Hi!
>
> On Tue, May 08, 2018 at 07:12:35AM +0000, Yuval Greenfield <
> ubershmekel at gmail.com> wrote:
> > If you
> > want to avoid duplicate `stat` calls, you'll probably write:
> >
> > import os
> > import stat
> > def is_hidden(st):
> > return bool(st.st_file_attributes & stat.FILE_ATTRIBUTE_HIDDEN)
> > def visible_walk(path):
> > for entry in os.scandir(path):
> > if entry.is_dir():
> > if not is_hidden(entry.stat()):
> > yield from visible_walk(entry.path)
> > else:
> > if not is_hidden(entry.stat()):
> > yield entry.path
>
> So anyone who wants to filter os.walk() must reimplement os.walk()
> themselves instead of passing something like filter_dir and filter_file
> (or accept_dir/accept_file) to os.walk()? Kind of painful, no?
>
> > Cheers,
> > Yuval
>
> Oleg.
> --
> Oleg Broytman http://phdru.name/ phd at phdru.name
> Programmers don't die, they just GOSUB without RETURN.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180508/f86f211b/attachment-0001.html>
More information about the Python-ideas
mailing list