xxxxxxxxxx
import shutil
shutil.rmtree(r'Path where the folder with its files is stored\Folder name')
xxxxxxxxxx
import os
import glob
files = glob.glob('/YOUR/PATH/*')
for f in files:
os.remove(f)
xxxxxxxxxx
>>> os.listdir()
['new_one', 'old.txt']
>>> os.remove('old.txt')
>>> os.listdir()
['new_one']
>>> os.rmdir('new_one')
>>> os.listdir()
[]
xxxxxxxxxx
import shutil
dir_path = '/tmp/img'
try:
shutil.rmtree(dir_path)
except OSError as e:
print("Error: %s : %s" % (dir_path, e.strerror))
xxxxxxxxxx
We can use functions from Python’s built-in os module to delete files and empty folders.
os.remove() will delete a file.
os.rmdir() will delete an empty folder.
To delete a folder that is not empty, we must use the rmtree() function from Python’s shutil module.
https://sentry.io/answers/delete-a-file-or-folder-in-python/