Python Forum

Full Version: How to skip a folder directory in a loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have a code below:

path = "C:\User\Work\Identity\TestFolders"

def list_files(dir):
    r = []
    skip = ["NetIQ"]
    for root, dirs, files in os.walk(dir):
        if dirs in skip:
            continue
        else:
            for name in files:
                r.append(os.path.join(root, name))
    return r

print(list_files(path))
I want to skip "NetIQ" folder in my iteration, but I think I'm missing something there, please help.
This won't work. dirs is a list of all directories at the current point of the os.walk(). Your comparison will never match, and if it did match it wouldn't do anything useful.

If you want to not process that directory AND not descend into that directory, the easiest way is to just remove it from dirs before you descend further (which is the default behavior).

Example of a 3 level directory walk
import os
for root, dirs, files in os.walk("."):
    for file in files:
        print(f"Dealing with file {root}/{file}")
Output:
Dealing with file ./a/1 Dealing with file ./a/b/2 Dealing with file ./a/b/c/3 Dealing with file ./a/bb/22 Dealing with file ./a/bb/cc/33
Lets say I want to ignore the directory "b" (and any children). I remove it from dirs (so the walk will not enter), and then I can just process the files as normal.

import os
for root, dirs, files in os.walk("."):
    if "b" in dirs:
        dirs.remove("b")
    for file in files:
        print(f"Dealing with file {root}/{file}")
Output:
Dealing with file ./treewalk.py Dealing with file ./a/1 Dealing with file ./a/bb/22 Dealing with file ./a/bb/cc/33
(Nov-18-2020, 07:34 AM)bowlofred Wrote: [ -> ]This won't work. dirs is a list of all directories at the current point of the os.walk(). Your comparison will never match, and if it did match it wouldn't do anything useful.

If you want to not process that directory AND not descend into that directory, the easiest way is to just remove it from dirs before you descend further (which is the default behavior).

Example of a 3 level directory walk
import os
for root, dirs, files in os.walk("."):
    for file in files:
        print(f"Dealing with file {root}/{file}")
Output:
Dealing with file ./a/1 Dealing with file ./a/b/2 Dealing with file ./a/b/c/3 Dealing with file ./a/bb/22 Dealing with file ./a/bb/cc/33
Lets say I want to ignore the directory "b" (and any children). I remove it from dirs (so the walk will not enter), and then I can just process the files as normal.

import os
for root, dirs, files in os.walk("."):
    if "b" in dirs:
        dirs.remove("b")
    for file in files:
        print(f"Dealing with file {root}/{file}")
Output:
Dealing with file ./treewalk.py Dealing with file ./a/1 Dealing with file ./a/bb/22 Dealing with file ./a/bb/cc/33

Hi,

Thanks for your response, it worked.