Removing hidden directory in os.walk()

Chris Rebert clp2 at rebertia.com
Fri Feb 25 18:52:23 CET 2011


On Fri, Feb 25, 2011 at 9:40 AM, Gaëtan Podevijn <gpodevij at gmail.com> wrote:
> Hello,
> I would like that os.walk() does not walk through hidden directories. I know
> that with topdow = true, I can modify the subdirectory list in place, but
> how should I remove every hidden directory from this place in list ?

Assuming you're on *nix:

for dirpath, dirnames, filenames in os.walk(top):
    dirnames[:] = [d for d in dirnames if not d.startswith('.')]
    do_whatever()

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list