From 586b615ecfb73130bc4f0317d47ffac53da59c05 Mon Sep 17 00:00:00 2001 From: Maxime Gasse Date: Wed, 30 Oct 2024 14:04:12 -0400 Subject: [PATCH] exp_dir sanitization (#222) --- browsergym/experiments/src/browsergym/experiments/loop.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/browsergym/experiments/src/browsergym/experiments/loop.py b/browsergym/experiments/src/browsergym/experiments/loop.py index 78bc1034..7cc12739 100644 --- a/browsergym/experiments/src/browsergym/experiments/loop.py +++ b/browsergym/experiments/src/browsergym/experiments/loop.py @@ -4,6 +4,7 @@ import logging import os import pickle +import re import sys import time import traceback @@ -179,13 +180,16 @@ def prepare(self, exp_root): def _make_dir(self, exp_root): """Create a unique directory for the experiment.""" date_str = self.exp_date.strftime("%Y-%m-%d_%H-%M-%S") + exp_str = re.sub( + r"[\/:*?<>|]", "_", self.exp_name + ) # sanitize exp_name to be used as a file name (substitute forbidden characters) for i in range(1000): if i >= 999: # make sure we don't loop forever raise ValueError("Could not find a unique name for the experiment directory.") tag = f"_{i}" if i > 0 else "" - self.exp_dir = Path(exp_root) / f"{date_str}_{self.exp_name}{tag}" + self.exp_dir = Path(exp_root) / f"{date_str}_{exp_str}{tag}" if not self.exp_dir.exists(): break