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)
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.
# Parent directory:
# Directory inside parent directory:
# File inside the sub-directory:
We want to remove the directory Authors. Below is the implementation.
Example 2: By passing
ignore_errors = False.
Traceback (most recent call last):
File “D:/Pycharm projects/gfg/gfg.py”, line 16, in
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 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 implementaton.
(, FileNotFoundError(2, ‘The system cannot find the path specified’), )
(, FileNotFoundError(2, ‘The system cannot find the file specified’), )
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Delete a directory or file using Python
- How to Retrieve an Entire Row or Column of an Array in Python?
- Rename all file names in your directory using Python
- Copy a directory recursively using Python (with examples)
- Get parent of current directory using Python
- Check if directory contains files using python
- Finding the largest file in a directory using Python
- Create a directory in Python
- Python - Check if a file or directory exists
- Python: Check if a File or Directory Exists
- Get directory of current Python script
- Python: Check if a directory is empty
- Change current working directory with Python
- Directory traversal tools in Python
- Implementing a Contacts directory in Python
- Python Directory Management
- How to Get directory of Current Script in Python?
- PYGLET – Getting Directory for Saving User Paths
- PYGLET – Getting Program Entry Directory
- Python | Delete rows/columns from DataFrame using Pandas.drop()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.