From 010334e2c9a2a318732901d8927d5bbf47234372 Mon Sep 17 00:00:00 2001 From: mouse Date: Tue, 21 Apr 2020 12:02:45 -0300 Subject: [PATCH] file error handling --- mkv_this/mkv_this.py | 25 +++++++++++++++++-------- setup.py | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/mkv_this/mkv_this.py b/mkv_this/mkv_this.py index 1cdd317..b0290a4 100755 --- a/mkv_this/mkv_this.py +++ b/mkv_this/mkv_this.py @@ -50,14 +50,19 @@ def main(): args = parser.parse_args() + fnf = 'error: file not found. please provide a path to a really-existing file!' + # if a combine file is provided, combine it w/ input file: if args.combine : # get raw text as a string for both files: - with open(args.infile, encoding="latin-1") as f: - text = f.read() - - with open(args.combine, encoding="latin-1") as cf: - ctext = cf.read() + try: + with open(args.infile, encoding="latin-1") as f: + text = f.read() + with open(args.combine, encoding="latin-1") as cf: + ctext = cf.read() + except FileNotFoundError: + print(fnf) + sys.exit() # build the models and build a combined model: # NB: attempting to implement Newline option here (and below): @@ -91,13 +96,17 @@ def main(): # if no combo file, just do normal: else: # Get raw text as string. - with open(args.infile, encoding="latin-1") as f: - text = f.read() + try: + with open(args.infile, encoding="latin-1") as f: + text = f.read() + except FileNotFoundError: + print(fnf) + sys.exit() # Build the model: # NB: this errors if infile is EMPTY: - ## implement newline option here: + ## newline option: if args.newline : text_model = markovify.NewlineText(text, state_size=args.state_size, well_formed=args.no_well_formed) diff --git a/setup.py b/setup.py index 7a1d3de..319bde6 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f: long_description = f.read() setup(name='mkv-this', - version='0.1.25', + version='0.1.26', description='cli wrapper for markovify: take a text file, markovify, output the results to a text file.', long_description=long_description, long_description_content_type='text/markdown',