0

How to remove only empty directories in Python

Python
import os

if not os.listdir(dir):
    os.rmdir(dir)

os.rmdir will not delete a directory that is not empty.

try:
    os.rmdir(dir)
except OSError as ex:
    if ex.errno == errno.ENOTEMPTY:
        print "directory not empty"
add comment
Suggest a different solution or add another example