diff --git a/cpdctrl/utility/data.py b/cpdctrl/utility/data.py index 61ae863..5022814 100644 --- a/cpdctrl/utility/data.py +++ b/cpdctrl/utility/data.py @@ -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)