Skip to content

Commit 72cc587

Browse files
author
Thierry RAMORASOAVINA
committed
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
1 parent 68f6798 commit 72cc587

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

khiops/core/internals/filesystems.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ def read(self, size=None):
463463
return local_file.read(size)
464464

465465
def write(self, data):
466+
directory = os.path.dirname(self.path)
467+
if len(directory) > 0 and not os.path.isdir(directory):
468+
os.makedirs(directory)
466469
with open(self.path, "wb") as output_file:
467470
output_file.write(data)
468471

tests/test_core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,11 @@ def test_dictionary(self):
303303
ref_kdic = os.path.join(ref_kdic_dir, f"{dictionary}.kdic")
304304
ref_kdicj = os.path.join(ref_kdicj_dir, f"{dictionary}.kdicj")
305305
output_kdic = os.path.join(output_kdic_dir, f"{dictionary}.kdic")
306+
output_kdic_in_a_new_folder = os.path.join(
307+
output_kdic_dir, "missing_folder", f"{dictionary}.kdic"
308+
)
306309
copy_output_kdic = os.path.join(copy_output_kdic_dir, f"{dictionary}.kdic")
310+
307311
with self.subTest(dictionary=dictionary):
308312
if dictionary in dictionaries_warn:
309313
with self.assertWarns(UserWarning):
@@ -312,6 +316,8 @@ def test_dictionary(self):
312316
domain = kh.read_dictionary_file(ref_kdicj)
313317
domain.export_khiops_dictionary_file(output_kdic)
314318
assert_files_equal(self, ref_kdic, output_kdic)
319+
domain.export_khiops_dictionary_file(output_kdic_in_a_new_folder)
320+
assert_files_equal(self, ref_kdic, output_kdic_in_a_new_folder)
315321

316322
domain_copy = domain.copy()
317323
domain_copy.export_khiops_dictionary_file(copy_output_kdic)

0 commit comments

Comments
 (0)