Skip to content

Commit 88e43eb

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 9de474c commit 88e43eb

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,14 @@ 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_new_dir = os.path.join(output_kdic_dir, "missing_folder")
307+
308+
# Cleanup previous `output_kdic_new_dir` folder
309+
shutil.rmtree(output_kdic_new_dir)
310+
311+
self.assertFalse(os.path.exists(output_kdic_new_dir))
306312
copy_output_kdic = os.path.join(copy_output_kdic_dir, f"{dictionary}.kdic")
313+
307314
with self.subTest(dictionary=dictionary):
308315
if dictionary in dictionaries_warn:
309316
with self.assertWarns(UserWarning):
@@ -312,6 +319,11 @@ def test_dictionary(self):
312319
domain = kh.read_dictionary_file(ref_kdicj)
313320
domain.export_khiops_dictionary_file(output_kdic)
314321
assert_files_equal(self, ref_kdic, output_kdic)
322+
domain.export_khiops_dictionary_file(
323+
os.path.join(output_kdic_new_dir, f"{dictionary}.kdic")
324+
)
325+
# Ensure a missing parent directory is created automatically
326+
self.assertTrue(os.path.exists(output_kdic_new_dir))
315327

316328
domain_copy = domain.copy()
317329
domain_copy.export_khiops_dictionary_file(copy_output_kdic)

0 commit comments

Comments
 (0)