From fe075c7c737b87d73d8527b15ef35c907574097c Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sun, 22 Mar 2020 21:35:36 +0100 Subject: [PATCH] Check if file exists and abort if t doesn't --- edit_data_form.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/edit_data_form.py b/edit_data_form.py index c4ca2d6..b158b23 100755 --- a/edit_data_form.py +++ b/edit_data_form.py @@ -4,7 +4,7 @@ import npyscreen import json import datetime -from sys import argv +import sys import pathlib from base_data_model import base_data_model @@ -57,18 +57,21 @@ class FormApp(npyscreen.NPSAppManaged): return pathlib.Path(filename).name == 'example.json' def main(*_args): - if len(argv) > 1: - filename = argv[1] + if len(sys.argv) > 1: + file_path = pathlib.Path(sys.argv[1]) + if not file_path.exists(): + print("Trying to open nonexistent file. Aborting.") + sys.exit(0) else: - filename = pathlib.Path(pathlib.Path(__file__).absolute().parent, 'example.json') + file_path = pathlib.Path(pathlib.Path(__file__).absolute().parent, 'example.json') - app = FormApp(filename) + app = FormApp(file_path) try: app.run() except Exception as e: print(e) - return 0 + sys.exit(0) if app.data_has_changed(): out_data = app.get_data_from_form()