Fix: Make sure dir exists when flushing

This commit is contained in:
CPD 2025-03-17 08:35:19 +01:00
parent 07828e41c9
commit 11dbf27da6

View File

@ -43,7 +43,7 @@ class DataCollector:
dirpath = f"{self.dirpath}-{i}"
print(f"Directory '{self.dirname}' already exists. Trying '{dirpath}' instead")
self.dirpath = dirpath
os.makedirs(self.dirpath)
self.assert_directory_exists()
self.flushed = False
@ -63,6 +63,11 @@ class DataCollector:
with open(filepath, "wb") as file:
pickle.dump(self.metadata, file)
def assert_directory_exists(self):
if not os.path.isdir(self.dirpath):
os.makedirs(self.dirpath)
def flush(self, verbose:bool=False):
"""
Write the current data to a file and clear the internal data
@ -85,7 +90,7 @@ class DataCollector:
# dont flush empty data
if len(self.data) == 0:
return
# TODO check if dir still exists
self.assert_directory_exists()
if FLUSH_TYPE == "csv":
filename = self._get_filename() + ".csv"
filepath = os.path.join(self.dirpath, filename)