Skip to content Skip to sidebar Skip to footer

Only Enumerate Files In Folders With Specific Names

I have a folder structure and files like this: - doe-john - inbox - 1. - 2. - 3. - sent - 1. - 2. - 3. - notes - 1. - 2.

Solution 1:

You're confusing root and dirs. root is the "current directory" at each level; dirs is a list of directories visible at this level.

Your current code processes all files in each directory, once per visible subdirectory. What you want is to see whether your current directory is inbox or sent, and only then do your processing.

for root, dirs, files inos.walk(top_level_folder):
    if root.lower().endswith("inbox") or root.lower().endswith("sent"):
        for fn in files:
            with open(os.path.join(root, fn), 'r') as f:
                pass  # do something

You can also set topdown=True in your walk call, and then modify which subdirectories you want to walk into.

for root, dirs, files in os.walk(top_level_folder, topdown=True):
    if root != top_level_folder:
        # only recurse into level 3+ directories with the desired names
        dirs[:] = [d for d in dirs if d in ['inbox', 'sent']]
    if root.lower().endswith("inbox") or root.lower().endswith("sent"):
        for fn in files:
            withopen(os.path.join(root, fn), 'r') as f:
                pass# do something

However, I find that option to be a little bit ugly (especially since you need a special case at the top level to avoid skipping /doe-john, etc.). In your particular case, since there are only two directories you want to look at and they're only one level down, I wouldn't use walk at all:

for person inos.listdir(top_level_folder):
    inbox = os.path.join(top_level_folder, person, 'inbox')
    sent = os.path.join(top_level_folder, person, 'sent')

    for file inos.listdir(inbox):
        pass # Do something

    for file inos.listdir(sent):
        pass # Do something

Solution 2:

You can modify the dirs returned from os.walk() if you use the topdown=True option. According to the documentation:

When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is False is ineffective, because in bottom-up mode the directories in dirnames are generated before dirpath itself is generated.

Post a Comment for "Only Enumerate Files In Folders With Specific Names"