I tried to implement the @VGE sentence, but my attempt was not too elegant. I would appreciate any suggestions for improving this.
import sys, fileinput, errno, os class nosuchfile: def readlines(foo, bar): return [] def close(arg): pass EXITCODE=0 def skip_on_error (filename, mode): """Function to pass in as fileinput.input(openhook=...) hook function. Instead of give up on the first error, skip the rest of the file and continue with the next file in the input list. In case of an error from open() an error message is printed to standard error and the global variable EXITCODE gets overwritten by a nonzero value. """ global EXITCODE try: return open(filename, mode) except IOError, e: sys.stderr.write ("%s: %s: %s\n" % (sys.argv[0], filename, os.strerror(e.errno))) EXITCODE = 1 return nosuchfile() def main (): do_stuff(fileinput.input(openhook=skip_on_error)) return EXITCODE
Both classes of the dummy descriptor file nosuchfile and the global variable EXITCODE are pretty serious warts. I tried to figure out how to pass the reference to the local variable exitcode, but refused.
It also prevents the handling of errors that occur during reading, but most error cases seem to occur in open anyway.
source share