pyhelpers.dirs.delete_dir(path_to_dir, confirmation_required=True, verbose=False, **kwargs)

Delete a directory or directories.

  • path_to_dir (str or bytes or os.PathLike[str] or os.PathLike[bytes] or – pathname (or pathnames) of a directory (or directories)

  • confirmation_required (bool) – whether to prompt a message for confirmation to proceed, defaults to True

  • verbose (bool or int) – whether to print relevant information in console as the function runs, defaults to False

  • kwargs – [optional] parameters of shutil.rmtree or os.rmdir


>>> from pyhelpers.dirs import cd, delete_dir
>>> import os

>>> test_dirs = []
>>> for x in range(3):
...     test_dirs.append(cd("tests", f"test_dir{x}", mkdir=True))
...     if x == 0:
...         cd("tests", f"test_dir{x}", "a_folder", mkdir=True)
...     elif x == 1:
...         open(cd("tests", f"test_dir{x}", "file"), 'w').close()

>>> delete_dir(path_to_dir=test_dirs, verbose=True)
To delete the following directories:
    "tests\test_dir0\" (Not empty)
    "tests\test_dir1\" (Not empty)
? [No]|Yes: yes
Deleting "tests\test_dir0\" ... Done.
Deleting "tests\test_dir1\" ... Done.
Deleting "tests\test_dir2\" ... Done.