The rmtree method of the shutil module
The rmtree method of the shutil module deletes a folder with all the files and folders it contains. In the first parameter of the method, we specify the path to the folder. In the second optional parameter, we specify ignore_errors. If its value is True, then errors that occur as a result of unsuccessful deletion will be ignored. In the third optional parameter, we specify oneerror. If its value is False, then the errors will be processed by calling the specified handler. If the deletion is successful, the method returns None.
Syntax
import shutil
shutil.rmtree(path to folder, [ignore_errors], [oneerror])
Example
Let's delete the dir folder with all its contents:
import shutil
print(shutil.rmtree('dir2'))
The result of the executed code:
None
See also
-
method
moveof moduleshutil,
which recursively moves a file or directory -
method
copytreeof moduleshutil,
which recursively copies a folder -
method
removeof moduleos,
which deletes the file -
method
makedirsof moduleos,
which creates folders -
method
renameof moduleos,
which renames a file and folder -
method
scandirof moduleos,
which iterates through the files in a folder