From bb6fb65ed832ae15a9640f4f137b91c64c6104d7 Mon Sep 17 00:00:00 2001 From: Paul Mure <57578250+paulmure@users.noreply.github.com> Date: Sat, 12 Dec 2020 10:54:24 -0800 Subject: [PATCH] Create directories before saving credentials This will prevent a FileNotFound error when the user first run the authentication process without creating the directory structure. --- td/client.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/td/client.py b/td/client.py index 087f9dc..37a46f0 100644 --- a/td/client.py +++ b/td/client.py @@ -219,7 +219,15 @@ def _state_manager(self, action: str) -> None: self.state.update(json.load(json_file)) # if they want to save it and have allowed for caching then load the file. - elif action == 'save': + elif action == 'save': + # Create parent directory if it doesn't exist + if not credentials_file_exists: + try: + os.makedirs(os.path.dirname(self.credentials_path)) + except OSError as exc: # Guard against race condition + if exc.errno != errno.EEXIST: + raise + # Save file with open(file=self.credentials_path, mode='w+') as json_file: json.dump(obj=self.state, fp=json_file, indent=4)