From aad0a7c3fbb94bd0d2d2b2e396821258ef24f0fd Mon Sep 17 00:00:00 2001 From: Florez Ospina Juan Felipe Date: Wed, 2 Oct 2024 13:54:59 +0200 Subject: [PATCH] Added file openning mode as input parameter. Now, mode can only take values in ['w','r+'] --- src/hdf5_lib.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/hdf5_lib.py b/src/hdf5_lib.py index 3bae37c..4c798db 100644 --- a/src/hdf5_lib.py +++ b/src/hdf5_lib.py @@ -86,7 +86,7 @@ def __copy_file_in_group(source_file_path, dest_file_obj : h5py.File, dest_group def create_hdf5_file_from_filesystem_path(path_to_input_directory: str, path_to_filenames_dict: dict = None, select_dir_keywords : list = [], - root_metadata_dict : dict = {}): + root_metadata_dict : dict = {}, mode = 'w'): """ Creates an .h5 file with name "output_filename" that preserves the directory tree (or folder structure) @@ -114,6 +114,9 @@ def create_hdf5_file_from_filesystem_path(path_to_input_directory: str, root_metadata_dict : dict Metadata to include at the root level of the HDF5 file. + mode : str + 'w' create File, truncate if it exists, or 'r+' read/write, File must exists. By default, mode = "w". + Returns ------- output_filename : str @@ -121,7 +124,9 @@ def create_hdf5_file_from_filesystem_path(path_to_input_directory: str, """ - + if not mode in ['w','r+']: + raise ValueError(f'Parameter mode must take values in ["w","r+"]') + if not '/' in path_to_input_directory: raise ValueError('path_to_input_directory needs to be specified using forward slashes "/".' ) @@ -142,7 +147,7 @@ def create_hdf5_file_from_filesystem_path(path_to_input_directory: str, root_dir = path_to_input_directory path_to_output_file = path_to_input_directory.rstrip(os.path.sep) + '.h5' - with h5py.File(path_to_output_file, mode='w', track_order=True) as h5file: + with h5py.File(path_to_output_file, mode=mode, track_order=True) as h5file: number_of_dirs = len(path_to_filenames_dict.keys()) dir_number = 1