From 8faa34b2ff09f3233eda82be9b138426777fbaea Mon Sep 17 00:00:00 2001 From: MMA <58857539+TheGeeKing@users.noreply.github.com> Date: Tue, 15 Aug 2023 18:08:20 +0200 Subject: [PATCH] Fix set_key creates orphan .env file Changed: - `set_key` now checks if the `dotenv_path` exists Fix https://github.com/theskumar/python-dotenv/issues/480 --- src/dotenv/main.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/dotenv/main.py b/src/dotenv/main.py index 383b79f4..90c07666 100644 --- a/src/dotenv/main.py +++ b/src/dotenv/main.py @@ -166,6 +166,10 @@ def set_key( or (quote_mode == "auto" and not value_to_set.isalnum()) ) + if not os.path.exists(dotenv_path): + logger.warning("Can't set key to %s - it doesn't exist.", dotenv_path) + return None, key_to_set, value_to_set + if quote: value_out = "'{}'".format(value_to_set.replace("'", "\\'")) else: