Added delete_item()
This commit is contained in:
parent
010ac87de6
commit
78e3765730
1 changed files with 20 additions and 3 deletions
|
|
@ -23,14 +23,31 @@ def delete_empty_folders(path):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def delete_folder(path, force=False):
|
def delete_folder(path, force=False, ignore_errors=False):
|
||||||
"""Delete folder if empty or if forced."""
|
"""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:
|
if force:
|
||||||
shutil.rmtree(path)
|
shutil.rmtree(path, ignore_errors=ignore_errors)
|
||||||
else:
|
else:
|
||||||
os.rmdir(path)
|
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):
|
def non_clobbering_path(path):
|
||||||
"""Update path as needed to non-existing path, returns pathlib.Path."""
|
"""Update path as needed to non-existing path, returns pathlib.Path."""
|
||||||
path = pathlib.Path(path)
|
path = pathlib.Path(path)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue