Advertisement
rfmonk

exceptions2.py

Dec 24th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. # e.g. from docs.python.org/2/tutorial/errors.html
  4.  
  5. import sys
  6.  
  7. try:
  8.     f = open('myfile.txt')
  9.     s = f.readline()
  10.     i = int(s.strip())
  11. except IOError as e:
  12.     print "I/O error({0}): {1}".format(e.errno, e.strerror)
  13. except ValueError:
  14.     print "Could not convert data to an integer."
  15. except:
  16.     print "Unexpected error:" sys.exc_info()[0]
  17.     raise
  18.  
  19. for arg in sys.argv[1:]:
  20.     try:
  21.         f = open(arg, 'r')
  22.     except IOError:
  23.         print 'cannot open', arg
  24.     else:
  25.         print arg, 'has', len(f.readlines()), 'lines'
  26.         f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement