import fileinput #prepare empty file to open in case of IOerror open('dummy', 'w').close() def hook_skip_IOerror(filename, mode): try: return open(filename, mode) except IOError: print("There was a problem with opening %s" % filename) return open('dummy', mode) def process(searchterm): for line in fileinput.input(openhook=hook_skip_IOerror): if line is not None: num_matches = line.count(searchterm) if num_matches: # a nonzero count means there was a match print "found '%s' %d times in %s on line %d." % (searchterm, num_matches, fileinput.filename(), fileinput.filelineno()) process('=')
Edited by Skrell: n/a
Edited by pyTony: n/a
Are you able to help answer this sponsored question?
Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.