From bc3237abd0742138b22b0d9e45719e6ca089302b Mon Sep 17 00:00:00 2001 From: Luciana Fujii Pontello Date: Thu, 27 Oct 2016 11:58:45 -0700 Subject: Handle exceptions when opening files and close them --- diff2HtmlCompare.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/diff2HtmlCompare.py b/diff2HtmlCompare.py index 266606a..63aa39b 100644 --- a/diff2HtmlCompare.py +++ b/diff2HtmlCompare.py @@ -204,14 +204,26 @@ class CodeDiff(object): self.filename = name self.fromfile = fromfile if fromtxt == None: - self.fromlines = open(fromfile, 'U').readlines() + try: + with open(fromfile) as f: + self.fromlines = f.readlines() + except Exception as e: + print "Problem reading file %s" % fromfile + print e + sys.exit(1) else: self.fromlines = [n + "\n" for n in fromtxt.split("\n")] self.leftcode = "".join(self.fromlines) self.tofile = tofile if totxt == None: - self.tolines = open(tofile, 'U').readlines() + try: + with open(tofile) as f: + self.tolines = f.readlines() + except Exception as e: + print "Problem reading file %s" % tofile + print e + sys.exit(1) else: self.tolines = [n + "\n" for n in totxt.split("\n")] self.rightcode = "".join(self.tolines) -- cgit