Check all folders if empty or not using python code

Hello Experts…i want to check if all folders (see image) is empty…I have tried checking online but i saw only checking 1 folder…I want all folders to check…Hoping for your support…thanks

image

What happens if you try using the code to check if a folder is empty, and repeating it for each of the folders you want to check? For example, by using a loop?

I am not really good in looping as of the moment…But really appreciate your message…I will study it now…thanks

I’d use os.walk()

Generates the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).

… in a nested for: loop:

 for path, dirs, files in walk(SRC):
    for file in files:
        ...

You can then check for file and do whatever you need to do.

You’ll need to learn, if you’re going to code Python.

1 Like

This is a little simpler than os.walk.

Using pathlib;

not any(Path('some/path/here').iterdir())

Combine that with something like this;

path = Path('dir')
for p in path.rglob("*"):
     print(p.name)

Edit:

I wanted to tack something on here.

Do this in two or maybe three pieces.

  1. Build the code to check if one directory is empty.
  2. Build code to either iterate over directories and do something simple like print out their name – or you could write a function to dynamically build a list of the directories that you want to check and loop through them doing something really simple like printing our their name.
  3. Combine what you need from step 2 with step 1.
1 Like