Added file openning mode as input parameter. Now, mode can only take values in ['w','r+']

This commit is contained in:
2024-10-02 13:54:59 +02:00
parent 4420f81642
commit aad0a7c3fb

View File

@ -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, def create_hdf5_file_from_filesystem_path(path_to_input_directory: str,
path_to_filenames_dict: dict = None, path_to_filenames_dict: dict = None,
select_dir_keywords : list = [], 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) 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 root_metadata_dict : dict
Metadata to include at the root level of the HDF5 file. 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 Returns
------- -------
output_filename : str 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: if not '/' in path_to_input_directory:
raise ValueError('path_to_input_directory needs to be specified using forward slashes "/".' ) 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 root_dir = path_to_input_directory
path_to_output_file = path_to_input_directory.rstrip(os.path.sep) + '.h5' 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()) number_of_dirs = len(path_to_filenames_dict.keys())
dir_number = 1 dir_number = 1