diff --git a/homeassistant/components/camera/__init__.py b/homeassistant/components/camera/__init__.py index 286c7957169..45d0cf9371a 100644 --- a/homeassistant/components/camera/__init__.py +++ b/homeassistant/components/camera/__init__.py @@ -849,8 +849,7 @@ async def async_handle_snapshot_service( """Executor helper to write image.""" if image_data is None: return - if not os.path.exists(os.path.dirname(to_file)): - os.makedirs(os.path.dirname(to_file), exist_ok=True) + os.makedirs(os.path.dirname(to_file), exist_ok=True) with open(to_file, "wb") as img_file: img_file.write(image_data) diff --git a/homeassistant/components/doods/image_processing.py b/homeassistant/components/doods/image_processing.py index e4398a756ec..cfc2b7c11c7 100644 --- a/homeassistant/components/doods/image_processing.py +++ b/homeassistant/components/doods/image_processing.py @@ -270,8 +270,7 @@ class Doods(ImageProcessingEntity): for path in paths: _LOGGER.info("Saving results image to %s", path) - if not os.path.exists(os.path.dirname(path)): - os.makedirs(os.path.dirname(path), exist_ok=True) + os.makedirs(os.path.dirname(path), exist_ok=True) img.save(path) def process_image(self, image): diff --git a/homeassistant/components/downloader/__init__.py b/homeassistant/components/downloader/__init__.py index 7cc64a1507b..8753d3e06f1 100644 --- a/homeassistant/components/downloader/__init__.py +++ b/homeassistant/components/downloader/__init__.py @@ -110,8 +110,7 @@ def setup(hass, config): subdir_path = os.path.join(download_path, subdir) # Ensure subdir exist - if not os.path.isdir(subdir_path): - os.makedirs(subdir_path) + os.makedirs(subdir_path, exist_ok=True) final_path = os.path.join(subdir_path, filename) diff --git a/homeassistant/components/gtfs/sensor.py b/homeassistant/components/gtfs/sensor.py index 99bc0ca51ed..f3e1d678d48 100644 --- a/homeassistant/components/gtfs/sensor.py +++ b/homeassistant/components/gtfs/sensor.py @@ -492,8 +492,7 @@ def setup_platform( offset: datetime.timedelta = config[CONF_OFFSET] include_tomorrow = config[CONF_TOMORROW] - if not os.path.exists(gtfs_dir): - os.makedirs(gtfs_dir) + os.makedirs(gtfs_dir, exist_ok=True) if not os.path.exists(os.path.join(gtfs_dir, data)): _LOGGER.error("The given GTFS data file/folder was not found") diff --git a/homeassistant/components/stream/recorder.py b/homeassistant/components/stream/recorder.py index 2fa612e631c..64a3208edcb 100644 --- a/homeassistant/components/stream/recorder.py +++ b/homeassistant/components/stream/recorder.py @@ -34,8 +34,7 @@ def recorder_save_worker(file_out: str, segments: deque[Segment]) -> None: _LOGGER.error("Recording failed to capture anything") return - if not os.path.exists(os.path.dirname(file_out)): - os.makedirs(os.path.dirname(file_out), exist_ok=True) + os.makedirs(os.path.dirname(file_out), exist_ok=True) pts_adjuster: dict[str, int | None] = {"video": None, "audio": None} output: OutputContainer | None = None diff --git a/homeassistant/components/tensorflow/image_processing.py b/homeassistant/components/tensorflow/image_processing.py index 323dbcb7882..9205b9ae561 100644 --- a/homeassistant/components/tensorflow/image_processing.py +++ b/homeassistant/components/tensorflow/image_processing.py @@ -327,8 +327,7 @@ class TensorFlowImageProcessor(ImageProcessingEntity): for path in paths: _LOGGER.info("Saving results image to %s", path) - if not os.path.exists(os.path.dirname(path)): - os.makedirs(os.path.dirname(path), exist_ok=True) + os.makedirs(os.path.dirname(path), exist_ok=True) img.save(path) def process_image(self, image): diff --git a/homeassistant/helpers/storage.py b/homeassistant/helpers/storage.py index 323737d5b98..da9aa06ba4a 100644 --- a/homeassistant/helpers/storage.py +++ b/homeassistant/helpers/storage.py @@ -277,8 +277,7 @@ class Store: def _write_data(self, path: str, data: dict) -> None: """Write the data.""" - if not os.path.isdir(os.path.dirname(path)): - os.makedirs(os.path.dirname(path)) + os.makedirs(os.path.dirname(path), exist_ok=True) _LOGGER.debug("Writing data for %s to %s", self.key, path) json_util.save_json( diff --git a/homeassistant/scripts/ensure_config.py b/homeassistant/scripts/ensure_config.py index b78b38735e1..f25639c7986 100644 --- a/homeassistant/scripts/ensure_config.py +++ b/homeassistant/scripts/ensure_config.py @@ -30,7 +30,7 @@ def run(args): # Test if configuration directory exists if not os.path.isdir(config_dir): print("Creating directory", config_dir) - os.makedirs(config_dir) + os.makedirs(config_dir, exist_ok=True) config_path = asyncio.run(async_run(config_dir)) print("Configuration file:", config_path) diff --git a/tests/components/homekit/test_homekit.py b/tests/components/homekit/test_homekit.py index 2d504ad517a..24b4cf5b373 100644 --- a/tests/components/homekit/test_homekit.py +++ b/tests/components/homekit/test_homekit.py @@ -1299,8 +1299,7 @@ async def test_homekit_uses_system_zeroconf(hass, hk_driver, mock_async_zeroconf def _write_data(path: str, data: dict) -> None: """Write the data.""" - if not os.path.isdir(os.path.dirname(path)): - os.makedirs(os.path.dirname(path)) + os.makedirs(os.path.dirname(path), exist_ok=True) json_util.save_json(path, data)