I've moved IOError handling to a separate function to avoid boilerplate when opening files for reading.
But what if IOError fires when the file is being read? If sshfs disconnects, or file is deleted by root, etc.?
def safe_open(*args):
try:
return open(*args)
except IOError:
quit('Error when opening file \'{0}\'. Error #{1[0]}: {1[1]}'.format(\
args[0], sys.exc_info()[1].args))
...
with safe_open(myfile, 'r') as f:
for i in f:
print i
with safe_open(anotherfile, 'r') as f:
try:
conf = ''.join(f).format(**args)
except KeyError:
quit('\nOops, your template \'{0}\' has placeholders for' + \
'parameters\nthat were not supplied in the command line: - {1}\n' +
'\nCan\'t proceed. Ending. Nothing has been changed yet.'.format( \
args['host_template'], '\n - '.join(sys.exc_info()[1].args)), 1)
File is read in different ways, so I don't see a way to put it into the function and pass the changing part as arguments.
[Added: thought of this solution, but it makes a generator that can't be closed. If a loop is stopped, the file is left open.]
def reader(*args):
try:
with safe_open(*args) as f:
for i in f:
yield i
except IOError:
print('IOError when trying to read \'{0}\''.format(args[0]))
for i in reader(myfile, 'r'):
pass # do some job