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

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically