From 78e3765730e2a61c8eae2c85a57925fc180839ac Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Sun, 15 Sep 2019 17:29:10 -0700 Subject: [PATCH] Added delete_item() --- scripts/wk/io.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/scripts/wk/io.py b/scripts/wk/io.py index d9637716..5ff75d7b 100644 --- a/scripts/wk/io.py +++ b/scripts/wk/io.py @@ -23,14 +23,31 @@ def delete_empty_folders(path): pass -def delete_folder(path, force=False): - """Delete folder if empty or if forced.""" +def delete_folder(path, force=False, ignore_errors=False): + """Delete folder if empty or if forced. + + NOTE: Exceptions are not caught by this function, + ignore_errors is passed to shutil.rmtree to allow partial deletions. + """ if force: - shutil.rmtree(path) + shutil.rmtree(path, ignore_errors=ignore_errors) else: os.rmdir(path) +def delete_item(path, force=False, ignore_errors=False): + """Delete file or folder, optionally recursively. + + NOTE: Exceptions are not caught by this function, + ignore_errors is passed to delete_folder to allow partial deletions. + """ + path = pathlib.Path(path) + if path.is_dir(): + delete_folder(path, force=force, ignore_errors=ignore_errors) + else: + os.remove(path) + + def non_clobbering_path(path): """Update path as needed to non-existing path, returns pathlib.Path.""" path = pathlib.Path(path)