Skip to content

Instantly share code, notes, and snippets.

@thigm85
Last active March 8, 2016 13:28
Show Gist options
  • Save thigm85/82dbe541f29b64e5d8ba to your computer and use it in GitHub Desktop.
Save thigm85/82dbe541f29b64e5d8ba to your computer and use it in GitHub Desktop.
Example of using else clause after try ... except clauses in python. Reference: https://docs.python.org/2/tutorial/errors.html#handling-exceptions
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment