Learned about hook functions for a thread having problem with bad user input from the documentation. This is result which skips bad inputs.
Fileinput from files ignoring incorrect ones.
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('=')
TrustyTony 888 pyMod Team Colleague Featured Poster
Skrell 0 Light Poster
TrustyTony 888 pyMod Team Colleague Featured Poster
Skrell 0 Light Poster
TrustyTony 888 pyMod Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.