delete_dir

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

Delete a directory or directories.

Parameters:
  • path_to_dir (str | bytes | os.PathLike[str] | os.PathLike[bytes] | collections.abc.Sequence) – Pathname(s) of the directory (or directories).

  • confirmation_required (bool) – Whether to prompt for confirmation before proceeding; defaults to True.

  • verbose (bool | int) – Whether to print relevant information to the console; defaults to False.

  • kwargs – [Optional] Additional parameters for the function shutil.rmtree or os.rmdir.

Examples:

>>> 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)
    "tests\test_dir2\"
? [No]|Yes: yes
Deleting "tests\test_dir0\" ... Done.
Deleting "tests\test_dir1\" ... Done.
Deleting "tests\test_dir2\" ... Done.