Skip to content

Migrate remove_empty_directories to lib.util.fs #1265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import sys

import lib.exitcode
import lib.utilities
from lib.dcm2bids_imaging_pipeline_lib.base_pipeline import BasePipeline
from lib.logging import log_error_exit
from lib.util.fs import remove_empty_directories

__license__ = "GPLv3"

Expand Down Expand Up @@ -268,6 +268,6 @@ def _clean_up_empty_folders(self):
# remove empty folders from file system
print("Cleaning up empty folders")
bids_cand_id = f"sub-{self.session.candidate.cand_id}"
lib.utilities.remove_empty_folders(os.path.join(self.data_dir, "assembly_bids", bids_cand_id))
lib.utilities.remove_empty_folders(os.path.join(self.data_dir, "pic", str(self.session.candidate.cand_id)))
lib.utilities.remove_empty_folders(os.path.join(self.data_dir, "trashbin"))
remove_empty_directories(os.path.join(self.data_dir, "assembly_bids", bids_cand_id))
remove_empty_directories(os.path.join(self.data_dir, "pic", str(self.session.candidate.cand_id)))
remove_empty_directories(os.path.join(self.data_dir, "trashbin"))
19 changes: 19 additions & 0 deletions python/lib/util/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,22 @@ def copy_file(env: Env, old_path: str, new_path: str):
shutil.copytree(old_path, new_path, dirs_exist_ok=True)
if not os.path.exists(new_path):
log_error_exit(env, f"Could not copy {old_path} to {new_path}", lib.exitcode.COPY_FAILURE)


def is_directory_empty(dir_path: str) -> bool:
"""
Check whether a directory is empty or not.
"""

with os.scandir(dir_path) as dir_iterator:
return next(dir_iterator, None) is None


def remove_empty_directories(dir_path: str):
"""
Recursively remove all the empty directories in a directory, including itself if needed.
"""

for subdir_path, _, _ in os.walk(dir_path, topdown=False):
if is_directory_empty(subdir_path):
os.rmdir(subdir_path)
1 change: 1 addition & 0 deletions python/lib/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ def create_processing_tmp_dir(template_prefix):
return tmp_dir


@deprecated('Use `lib.util.fs.remove_empty_subdirectories` instead')
def remove_empty_folders(path_abs):

walk = list(os.walk(path_abs))
Expand Down
Loading