Skip to content

Commit

Permalink
Add: Verbose option in safe_rmdir func.
Browse files Browse the repository at this point in the history
  • Loading branch information
Labbeti committed Jan 23, 2024
1 parent 1aa30d7 commit 36a60ac
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/aac_datasets/utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from pathlib import Path
from typing import List, Union

import tqdm

from torch.hub import download_url_to_file


Expand All @@ -33,25 +35,29 @@ def safe_rmdir(
rm_root: bool = True,
error_on_non_empty_dir: bool = True,
followlinks: bool = False,
verbose: int = 0,
) -> List[str]:
"""Remove all empty sub-directories.
:param root: Root directory path.
:param rm_root: If True, remove the root directory too. defaults to True.
:param error_on_non_empty_dir: If True, raises a RuntimeError if a subdirectory contains at least 1 file. Otherwise it will leave non-empty directories. defaults to True.
:param followlinks: Indicates whether or not symbolic links shound be followed. defaults to False.
:param verbose: Verbose level. defaults to 0.
:returns: The list of directories paths deleted.
"""
to_delete = []

for dpath, dnames, fnames in os.walk(root, topdown=False, followlinks=followlinks):
for dpath, dnames, fnames in tqdm.tqdm(
os.walk(root, topdown=False, followlinks=followlinks),
disable=verbose < 2,
):
if not rm_root and dpath == root:
continue
elif len(fnames) == 0 and len(dnames) == 0:
to_delete.append(dpath)
elif error_on_non_empty_dir:
print(f"{to_delete=}")
raise RuntimeError(f"Cannot remove non-empty directory {dpath}.")
raise RuntimeError(f"Cannot remove non-empty directory '{dpath}'.")

for dpath in to_delete:
os.rmdir(dpath)
Expand Down

0 comments on commit 36a60ac

Please sign in to comment.