Added delete_item()

This commit is contained in:
2Shirt 2019-09-15 17:29:10 -07:00
parent 010ac87de6
commit 78e3765730
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -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)