Change language

Delete an entire directory tree with Python | shutil.rmtree () method

|

shutil.rmtree () is used to delete an entire directory tree, the path must point to a directory (not a symbolic link to a directory).

Syntax : shutil.rmtree (path, ignore_errors = False, onerror = None)

Parameters:
path: A path -like object representing a file path. A path-like object is either a string or bytes object representing a path.
ignore_errors: If ignore_errors is true, errors resulting from failed removals will be ignored.
oneerror: If ignore_errors is false or omitted, such errors are handled by calling a handler specified by onerror.

Example 1. and the subdirectories are as follows.

# Parent directory:

# Directory inside parent directory:

# File inside subdirectory:

We want to delete the authors directory. Below is the implementation.

# Python program for demonstration
# shutil.rmtree ()

 

import shutil 

import os 

 
# location

location = "D: / Pycharm projects / Python.Engineering /"

 
# catalog

dir = "Authors"

 
# path

path = os.path.join ( (location, dir

  
# deleting a directory
shutil.rmtree (path) 

Exit:

Example 2: by passing ignore_errors = False .

# Python program for demonstration
# shutil.rmtree ()

 

import shutil 

import os 

  
# location

location = "D: / Pycharm projects / Python.Engineering /"

  
# catalog

dir = "Authors"

 
# path

path = os.path.join ( (location, dir

  
# deleting the directory

shutil.rmtree (path, ignore_errors = False

  
# make ignore_errors = True won’t boost
# a FileNotFoundError

Exit:

Traceback (most recent call last):
File “D: / Pycharm projects / gfg / gfg.py”, line 16, in
shutil.rmtree (path, ignore_errors = False)
File “C: UsersNikhil AggarwalAppDataLocalProgramsPythonPython38-32libshutil.py”, line 730, in rmtree
return _rmtree_unsafe (path, onerror)
File “C: UsersNikhil, AggarwalAppDataLocalPrograms38-Python _rmtree_unsafe
onerror (os.scandir, path, sys.exc_info ())
File “C: UsersNikhil AggarwalAppDataLocalProgramsPythonPython38-32libshutil.py”, line 586, in _rmtree_unsafe
(with path os.scandir ) as scandir_it:
FileNotFoundError: [WinError 3] The system cannot find the path specified: ’D: / Pycharm projects / Python.Engineering / Authors’

  Example 3: passing onerror
A function must be passed to onerror , which must contain three parameters.

  • function: function that caused the exception.
  • path: passed the path that caused the deletion exception
  • excinfo: information about the exception called by sys.exc_info ()

Below is a description.

# Python program for demonstration
# shutil.rmtree ()

  

import shutil 

import os 

 

 
# exception handler

def handler (func, path, exc_info): 

print ( "Inside handler"

print (exc_info) 

 

 
# place location

location = "D: / Pycharm projects / Python.Engineering / "

  
# directory

dir = "Authors"

 
# path

path = os.path.join ( (location, dir

 
# deleting directory

shutil.rmtree (path, onerror = handler) 

Exit:

Inside handler
(, FileNotFoundError (2, ’The system cannot find the path specified’),)
Inside handler
(, FileNotFoundError (2, ’The system cannot find the file specified’), )

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method