delete empty folder python code example

Example 1: delete folder and its subfolders in python

import shutil
shutil.rmtree('/path/to/your/dir/')

Example 2: delete contents of directory python

import os
import glob

files = glob.glob('/YOUR/PATH/*')
for f in files:
    os.remove(f)

Example 3: python remove empty folders

import os
def drop_empty_folders(directory):
    """Verify that every empty folder removed in local storage."""

    for dirpath, dirnames, filenames in os.walk(directory, topdown=False):
        if not dirnames and not filenames:
            os.rmdir(dirpath)