Python Forum
How to skip a folder directory in a loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to skip a folder directory in a loop
#1
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.
Reply
#2
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
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through all files in a directory? Winfried 10 316 Yesterday, 07:38 PM
Last Post: FortuneCoins
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 551 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  How to loop through all excel files and sheets in folder jadelola 1 4,497 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,495 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  need to skip password prompt, continue... tester_V 2 1,472 Oct-19-2021, 05:49 PM
Last Post: tester_V
  Delimiters - How to skip some html tags from being translate Melcu54 0 1,657 May-26-2021, 06:21 AM
Last Post: Melcu54
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,478 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  How to skip to a different line while importing a different script kat_gamer 2 2,246 Feb-03-2021, 04:10 AM
Last Post: deanhystad
  How to skip LinkedIn signup link using python script? Mangesh121 0 1,796 Aug-26-2020, 01:22 PM
Last Post: Mangesh121
  Python Cut/Copy paste file from folder to another folder rdDrp 4 5,057 Aug-19-2020, 12:40 PM
Last Post: rdDrp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020