From 886cc017abc81805f314aab71e50ff3b8fccd9f4 Mon Sep 17 00:00:00 2001 From: smol snail <6765737-smolsnail@users.noreply.gitlab.com> Date: Tue, 9 Mar 2021 14:36:40 -0800 Subject: [PATCH] Add error handling --- pat.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pat.py b/pat.py index e475c0e..310c33b 100755 --- a/pat.py +++ b/pat.py @@ -1,10 +1,11 @@ #!/usr/bin/python -from sys import stdin, argv +from sys import argv, stdin, stderr from string import whitespace # These next 2 lines of code are only for ANSI escape support on Windows cmd prompt/powershell # If you do not use cmd/ps, you can comment them out or remove them from os import system + system("") color_info = {"amount": 299, "max_rgb_value": 255} @@ -15,8 +16,15 @@ if len(argv) == 1: argv.append("-") for argument in argv[1:]: - read_result = stdin.read() if argument == "-" else open(argument, "r").read() - color_info["changes"] = len(list(filter(lambda c: not c in whitespace, read_result))) - 1 + try: + read_result = stdin.read() if argument == "-" else open(argument, "r").read() + except Exception as e: + print(e, file=stderr) + continue + + color_info["changes"] = ( + len(list(filter(lambda c: not c in whitespace, read_result))) - 1 + ) color_info["step"] = ( 0 if color_info["changes"] == 0