How to run python script for every folder in directory and skip any empty folders?

Viewed 157

I am trying to figure out how to generate an excel workbook for each subfolder in my directory while skipping the folders that are empty. My directory structure is below.

So it would start with Folder A, execute my lines of code to create an excel file using Folder A's contents, then move to Folder B, execute my lines of code to create a separate excel file using Folder B's contents, then move to Folder C and skip it since it's empty, and continue on.

How do I loop through each folder in this manner and keep going when a folder is empty?

I would greatly appreciate the help!

myscript.py  
folderA
    - report1.xlsx
    - report2.xlsx
folderB
    - report1.xlsx
    - report2.xlsx
folderC 
    ** EMPTY **    
folderD 
    - report1.xlsx
    - report2.xlsx
2 Answers

Something like this maybe?

from pathlib import Path
from itertools import groupby

def by_folder(path):
    return path.parent

for folder, files in groupby(Path("path/to/root/dir").rglob("*.xlsx"), key=by_folder):
    print(f"Gonna merge these files from {folder}: ")
    for file in files:
        print(f"{file.name}")
    print()

We recursively search for .xlsx files in the root directory, and group files into lists based on the immediate common parent folder. If a folder is empty, it won't be matched by the glob pattern.

You can use the os.listdir() method to list everything that's inside a folder. Only bad thing is that it'll get all files, so you may get a problem to get "inside a file" when actually you want to get inside all folder.

The following code loop through all subfolders, skip files, and print the name of all folders that are not empty.

for folder in os.listdir("."):
    try: 
        if len( os.listdir("./"+folder) )>0:
            print(
                folder
            )
    except:
        pass
Related