Handle exceptions when opening files and close them
This commit is contained in:
parent
7608485200
commit
bc3237abd0
1 changed files with 14 additions and 2 deletions
|
|
@ -204,14 +204,26 @@ class CodeDiff(object):
|
||||||
self.filename = name
|
self.filename = name
|
||||||
self.fromfile = fromfile
|
self.fromfile = fromfile
|
||||||
if fromtxt == None:
|
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:
|
else:
|
||||||
self.fromlines = [n + "\n" for n in fromtxt.split("\n")]
|
self.fromlines = [n + "\n" for n in fromtxt.split("\n")]
|
||||||
self.leftcode = "".join(self.fromlines)
|
self.leftcode = "".join(self.fromlines)
|
||||||
|
|
||||||
self.tofile = tofile
|
self.tofile = tofile
|
||||||
if totxt == None:
|
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:
|
else:
|
||||||
self.tolines = [n + "\n" for n in totxt.split("\n")]
|
self.tolines = [n + "\n" for n in totxt.split("\n")]
|
||||||
self.rightcode = "".join(self.tolines)
|
self.rightcode = "".join(self.tolines)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue