Deleting file/dir using the shutil.rmtree()
shutil.rmtree() is used to delete an entire directory tree, a path must point to a directory (but not a symbolic link to a directory).
Syntax of shutil.rmtree()
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.
- onerror: If ignore_errors is false or omitted, such errors are handled by calling a handler specified by onerror.
Delete a directory and the files contained in it.
Example 1:
Suppose the directory and sub-directories are as follow.
# Parent directory:
# Directory inside parent directory:
# File inside the sub-directory:
Example: Delete all Files from a 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/w3wiki/" # directory dir = "Authors" # path path = os.path.join(location, dir ) # removing directory shutil.rmtree(path) |
Output:
Example 2: Ignore error while deleting a directory
By passing ignore_errors = True.
Python3
# Python program to demonstrate # shutil.rmtree() import shutil import os # location location = "D:/Pycharm projects/w3wiki/" # 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/w3wiki/Authors’
Example 3: Exception handler
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/w3wiki/" # 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’), )
Delete a directory or file using Python
In this article, we will cover how to delete (remove) files and directories in Python. Python provides different methods and functions for removing files and directories. One can remove the file according to their need.
Various methods provided by Python are –
- Using os.remove()
- Using os.rmdir()
- Using shutil.rmtree()
- Using pathlib.Path(empty_dir_path).rmdir()