Removing a Directory

shutil.rmtree() is used to delete an entire directory tree, the 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.

Python3




# Python program to demonstrate
# shutil.rmtree()
 
import shutil
import os
 
# location
location = "csv/gfg/"
 
# directory
dir = "dest"
 
# path
path = os.path.join(location, dir)
 
# removing directory
shutil.rmtree(path)


Shutil Module in Python

Shutil module offers high-level operation on a file like a copy, create, and remote operation on the file. It comes under Python’s standard utility modules. This module helps in automating the process of copying and removal of files and directories. In this article, we will learn this module.

Similar Reads

Copying Files to another directory

shutil.copy() method in Python is used to copy the content of the source file to the destination file or directory. It also preserves the file’s permission mode but other metadata of the file like the file’s creation and modification times is not preserved.The source must represent a file but the destination can be a file or a directory. If the destination is a directory then the file will be copied into the destination using the base filename from the source. Also, the destination must be writable. If the destination is a file and already exists then it will be replaced with the source file otherwise a new file will be created....

Copying the Metadata along with File

...

Copying the content of one file to another

...

Replicating complete Directory

shutil.copy2() method in Python is used to copy the content of the source file to the destination file or directory. This method is identical to shutil.copy() method but it also tries to preserve the file’s metadata....

Removing a Directory

...

Finding files

...