Return to Snippet

Revision: 70227
at December 13, 2015 05:36 by ManishChandraReddy


Initial Code
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()

Initial URL
https://docs.python.org/2/tutorial/errors.html

Initial Description
The try ... except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception. For example:

Initial Title
Try Except Else

Initial Tags
error

Initial Language
Python