0

I have in a dictionary stored a list of directories in which I don't want to walk in. Using os.walk(), is there a way to stop going further down once we come to know that the directory is in the dictionary? For instance, if I have dict = {"a/b/c":1}, and I have used os.walk("a"), I don't want to walk in "a/b/c" directory.

Dhruv Mullick
  • 551
  • 9
  • 25
  • 2
    See the duplicate on how to skip a directory and all subdirectories; you remove it from the `dirs` list. Combine the name in `dirs` with the `dir` value to create a full path, test that against your dictionary. `dirs[:] = [d for d in dirs if os.path.join(dir, d) not in dictionary]` would filter any full path present in `dictionary` from the `dirs` list in such a way that `os.walk()` will skip them. – Martijn Pieters Mar 15 '16 at 09:54
  • Thank you, this really helped – Dhruv Mullick Mar 15 '16 at 10:22

0 Answers0