Links

Delete entire directory tree

Tags: #python #shutil #delete #folder #file #directory
Author: Florent Ravenel
Last update: 2023-04-26 (Created: 2023-04-26)
Description: This notebook will show how to delete an entire directory tree using the shutil library.
Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating the process of copying and removal of files and directories.
shutil.rmtree() is used to delete an entire directory tree, path must point to a directory (but not a symbolic link to a directory).
References:

Input

Import libraries

import shutil

Setup Variables

  • folder_path: path of the folder to be removed
folder_path = "./my_folder"

Model

Delete entire directory tree

shutil.rmtree(folder_path)

Output

Display result

print(f"Folder {folder_path} deleted.")