From 72cc58764a6da264013d70ea11f9be8bcd85287f Mon Sep 17 00:00:00 2001 From: Thierry RAMORASOAVINA Date: Mon, 23 Mar 2026 15:06:48 +0100 Subject: [PATCH] Fix and make `LocalFilesystemResource.write` consistent with the other FS (GCS, S3) Before writing the target file, if a folder is missing in the file path, it is automatically created --- khiops/core/internals/filesystems.py | 3 +++ tests/test_core.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/khiops/core/internals/filesystems.py b/khiops/core/internals/filesystems.py index bde17c18..2698844c 100644 --- a/khiops/core/internals/filesystems.py +++ b/khiops/core/internals/filesystems.py @@ -463,6 +463,9 @@ def read(self, size=None): return local_file.read(size) def write(self, data): + directory = os.path.dirname(self.path) + if len(directory) > 0 and not os.path.isdir(directory): + os.makedirs(directory) with open(self.path, "wb") as output_file: output_file.write(data) diff --git a/tests/test_core.py b/tests/test_core.py index 34b37456..d469889d 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -303,7 +303,11 @@ def test_dictionary(self): ref_kdic = os.path.join(ref_kdic_dir, f"{dictionary}.kdic") ref_kdicj = os.path.join(ref_kdicj_dir, f"{dictionary}.kdicj") output_kdic = os.path.join(output_kdic_dir, f"{dictionary}.kdic") + output_kdic_in_a_new_folder = os.path.join( + output_kdic_dir, "missing_folder", f"{dictionary}.kdic" + ) copy_output_kdic = os.path.join(copy_output_kdic_dir, f"{dictionary}.kdic") + with self.subTest(dictionary=dictionary): if dictionary in dictionaries_warn: with self.assertWarns(UserWarning): @@ -312,6 +316,8 @@ def test_dictionary(self): domain = kh.read_dictionary_file(ref_kdicj) domain.export_khiops_dictionary_file(output_kdic) assert_files_equal(self, ref_kdic, output_kdic) + domain.export_khiops_dictionary_file(output_kdic_in_a_new_folder) + assert_files_equal(self, ref_kdic, output_kdic_in_a_new_folder) domain_copy = domain.copy() domain_copy.export_khiops_dictionary_file(copy_output_kdic)