Question

I am trying to remove the empty folders of a directory.

def remove_empty_dir(path):
    try:
        os.rmdir(path)
    except OSError:
        pass

def remove_empty_dirs(path):
    for root, dirnames, filenames in os.walk(path):
        for dirname in dirnames:
            remove_empty_dir(os.path.realpath(os.path.join(root, dirname)))

remove_empty_dirs(path)

I have also tried with:

import shutil
shutil.rmtree(path)

But that removes everything, even those folders with contents. The problem is that I need to do it from inside to outside, this way if I have:

root_folder
  child_folder1
    grandchild_folder1.1 (empty)
  child_folder2
    granchild_folder2.1
    granchild_folder2.2 (empty)

The program should delete grandchild_folder1.1, child_folder1 and child_folder2.2, but not the rest.

Was it helpful?

Solution

os.walk accepts optional topdown parameter (default: True).

By providing topdown=False, you can iterative from child directories first.

def remove_empty_dirs(path):
    for root, dirnames, filenames in os.walk(path, topdown=False):
        for dirname in dirnames:
            remove_empty_dir(os.path.realpath(os.path.join(root, dirname)))

OTHER TIPS

Using the pathlib library in Python 3 this is a one-liner (other than includes). In the snippet below target_path is a string of the root of the tree you want to clean up:

from pathlib import Path
import os

[os.removedirs(p) for p in Path(target_path).glob('**/*') if p.is_dir() and len(list(p.iterdir())) == 0]

To make it a little less dense and easier to follow, this is the same thing written without the list comprehension

for p in Path(target_path).glob('**/*'):
    if p.is_dir() and len(list(p.iterdir())) == 0:
        os.removedirs(p)

The interesting feature here is the if statement filters for empty directories that are leaves on the filesystem tree. os.removedirs() deletes all empty folders in above an empty leaf. If there are several empty leaves on a branch, deleting the last empty leaf will cause os.removedirs() to walk up the branch. So all empty dirs are gone in a single iteration of the loop with no recursion necessary!

def remove_empty_dirs(dir_path):
    p1 = subprocess.Popen(["find", dir_path, "-type", "d", "-empty", "-print0"], stdout=subprocess.PIPE,)
    p2 = subprocess.Popen(["xargs", "-0", "-r", "rmdir"], stdin=p1.stdout, stdout=subprocess.PIPE)
    p1.stdout.close()
    p2.communicate()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top