Use else in exception handle : Try Else « Language Basics « Python






Use else in exception handle

import sys

for arg in sys.argv[1:]:
    try:
        f = open(arg, 'r')
    except IOError:
        print 'cannot open', arg
    else:
        print arg, 'has', len(f.readlines()), 'lines'
        f.close()


           
       








Related examples in the same category

1.try with else statementtry with else statement