In [1]:
import os

In [2]:
os.makedirs('temp/dir_empty/', exist_ok=True)

In [3]:
print(os.listdir('temp/'))


['dir_empty']

In [4]:
os.rmdir('temp/dir_empty/')

In [5]:
print(os.listdir('temp/'))


[]

In [6]:
os.makedirs('temp/dir_not_empty/', exist_ok=True)

In [7]:
with open('temp/dir_not_empty/file.txt', 'w') as f:
    f.write('')

In [8]:
# os.rmdir('temp/dir_not_empty/')
# OSError: [Errno 66] Directory not empty: 'temp/dir_not_empty/'

In [9]:
try:
    os.rmdir('temp/dir_not_empty/')
except OSError as e:
    pass

In [10]:
import shutil

In [11]:
shutil.rmtree('temp/dir_not_empty/')

In [12]:
print(os.listdir('temp/'))


[]