get_ext

Contents

get_ext#

get_ext(file_path: str | CloudPath | Path, curate_dir_extensions=True, start_with_point: bool = True) str[source]#

Get file extension from file path.

Warning

  • Extension is given WITHOUT THE FIRST POINT if start_with_point is set to False

  • Handle chained extensions with gz only.

  • Therefore zipped directory extensions such as SAFE or SEN3 will be ignored (.SAFE.zip will return .zip). This is wanted.

  • Only accept SAFE, SEN3, data in existing directory extensions if curate_dir_extensions is True

Parameters:
  • file_path (AnyPathStrType) – Absolute or relative file path (the file doesn’t need to exist)

  • curate_dir_extensions (bool) – If True, only accept SAFE, SEN3, data in existing directory extensions

  • start_with_point (bool) – Extensions starts with a point to follow pathlib’s logic. Set it to False to get back to older behaviour

Returns:

File or directory extension

Return type:

str

Examples

>>> path = = 'D:/path/to/filename.zip'
>>> get_ext(path)
'.zip'
>>> path = = 'D:/path/to/filename.zip'
>>> get_ext(path, start_with_point=False)
'zip'
>>> path = = 'D:/path/to/filename.tar.gz'
>>> get_ext(path)
'.tar.gz'
>>> path = = 'D:/S1A_IW_GRDH_1SDV_20260523T071052_20260523T071117_064641_082495_04CE.SAFE.zip'
>>> get_ext(path)
'.zip'
>>> path = 'D:/S1A_IW_GRDH_1SDV_20260523T071052_20260523T071117_064641_082495_04CE.SAFE'
>>> get_ext(path)
'.SAFE'
>>> path = 'D:/HLS.L30.T42RVR.2022240T055634.v2.0' # Has to truly exist
>>> get_ext(path)
''
>>> path = 'D:/HLS.L30.T42RVR.2022240T055634.v2.0'
>>> get_ext(path, curate_dir_extensions=False)
'.0'
>>> path = 'D:/HLS.L30.T42RVR.2022240T055634.v2.0.B01.tif' # Has to truly exist
>>> get_ext(path)
'.tif'