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"