delete_dir¶
- pyhelpers.dirs.delete_dir(dir_path, confirmation_required=True, verbose=False, indent=2, raise_error=False, **kwargs)[source]¶
Delete a directory or multiple directories.
- Parameters:
dir_path (str | bytes | os.PathLike | collections.abc.Sequence) – Pathname(s) of the directory (or directories) to be deleted.
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.indent (int | str) – Indentation level; if an integer, represents the number of spaces; if a string, used as the indentation character (e.g.
'\t'). Defaults to2(two spaces).raise_error (bool) – Whether to raise an exception if deletion fails; if
raise_error=False(default), the error will be suppressed. Defaults toFalse.kwargs – Optional parameters passed directly to shutil.rmtree (the underlying deletion function).
Examples:
>>> from pyhelpers.dirs import cd, delete_dir, get_relative_path >>> 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() >>> for x in test_dirs: ... print(get_relative_path(x)) tests est_dir0 tests est_dir1 tests est_dir2 >>> delete_dir(test_dirs, verbose=True) Confirm deletion of the following directories: "tests/test_dir0/" (Not empty) "tests/test_dir1/" (Not empty) "tests/test_dir2/" ? [No]|Yes: yes Deleting: "tests/test_dir0/" ... Done. "tests/test_dir1/" ... Done. "tests/test_dir2/" ... Done.