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

Oleg Broytman phd at phdru.name
Tue May 8 05:31:20 EDT 2018


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 Broytman            http://phdru.name/            phd at phdru.name
           Programmers don't die, they just GOSUB without RETURN.

More information about the Python-ideas mailing list