(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}")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.
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
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.