Delete an entire directory tree using Python | shutil.rmtree() method
Last Updated :
05 Jul, 2021
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).
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: Suppose the directory and sub-directories are as follow.
# Parent directory:
# Directory inside parent directory:
# File inside the sub-directory:
We want to remove the directory Authors. Below is the implementation.
Python3
# Python program to demonstrate
# shutil.rmtree()
import shutil
import os
# location
location = "D:/Pycharm projects/GeeksforGeeks/"
# directory
dir = "Authors"
# path
path = os.path.join(location, dir)
# removing directory
shutil.rmtree(path)
Output:
Example 2: By passing ignore_errors = False.
Python3
# Python program to demonstrate
# shutil.rmtree()
import shutil
import os
# location
location = "D:/Pycharm projects/GeeksforGeeks/"
# directory
dir = "Authors"
# path
path = os.path.join(location, dir)
# removing directory
shutil.rmtree(path, ignore_errors = False)
# making ignore_errors = True will not raise
# a FileNotFoundError
Output:
Traceback (most recent call last):
File “D:/Pycharm projects/gfg/gfg.py”, line 16, in
shutil.rmtree(path, ignore_errors=False)
File “C:\Users\Nikhil Aggarwal\AppData\Local\Programs\Python\Python38-32\lib\shutil.py”, line 730, in rmtree
return _rmtree_unsafe(path, onerror)
File “C:\Users\Nikhil Aggarwal\AppData\Local\Programs\Python\Python38-32\lib\shutil.py”, line 589, in _rmtree_unsafe
onerror(os.scandir, path, sys.exc_info())
File “C:\Users\Nikhil Aggarwal\AppData\Local\Programs\Python\Python38-32\lib\shutil.py”, line 586, in _rmtree_unsafe
with os.scandir(path) as scandir_it:
FileNotFoundError: [WinError 3] The system cannot find the path specified: ‘D:/Pycharm projects/GeeksforGeeks/Authors’
Example 3: By passing onerror.
In onerror a function should be passed which must contain three parameters.
- function: function which raised the exception.
- path: path name passed which raised the exception while removal
- excinfo: exception info raised by sys.exc_info()
Below is the implementation.
Python3
# Python program to demonstrate
# shutil.rmtree()
import shutil
import os
# exception handler
def handler(func, path, exc_info):
print("Inside handler")
print(exc_info)
# location
location = "D:/Pycharm projects/GeeksforGeeks/"
# directory
dir = "Authors"
# path
path = os.path.join(location, dir)
# removing directory
shutil.rmtree(path, onerror = handler)
Output:
Inside handler
(, FileNotFoundError(2, ‘The system cannot find the path specified’), )
Inside handler
(, FileNotFoundError(2, ‘The system cannot find the file specified’), )
Similar Reads
How to Delete files in Python using send2trash module? In this article, we will see how to safely delete files and folders using the send2trash module in Python. Using send2trash, we can send files to the Trash or Recycle Bin instead of permanently deleting them. The OS module's unlink(), remove() and rmdir() functions can be used to delete files or fol
2 min read
Difference Between os.rename and shutil.move in Python When it comes to renaming or moving files and directories, two commonly used methods are os. rename and shutil. move. While both can serve the purpose of renaming and moving, they have distinct differences in terms of functionality and use cases. In this article, we will explore these differences an
3 min read
Python - Read file from sibling directory In this article, we will discuss the method to read files from the sibling directory in Python. First, create two folders in a root folder, and one folder will contain the python file and the other will contain the file which is to be read. Below is the dictionary tree: Directory Tree: root : | |__S
3 min read
List all files of certain type in a directory using Python In python, there are several built-in modules and methods for file handling. These functions are present in different modules such as os, glob, etc. This article helps you find out many of the functions in one place which gives you a brief knowledge about how to list all the files of a certain type
3 min read
How to move all files from one directory to another using Python ? In this article, we will see how to move all files from one directory to another directory using Python. Â In our day-to-day computer usage we generally copy or move files from one folder to other, now let's see how to move a file in Python: This can be done in two ways:Using os module.Using shutil m
2 min read
How to print all files within a directory using Python? The OS module is one of the most popular Python modules for automating the systems calls and operations of an operating system. With a rich set of methods and an easy-to-use API, the OS module is one of the standard packages and comes pre-installed with Python. In this article, we will learn how to
3 min read