[Tutor] IOError exception handling

Allyn Weaks allyn.@tardigrade.net
Sun, 28 Jul 2002 16:33:22 -0700

Python 2.1 until I can get 2.2.1 installed properly (linux 7.1), but
I'll also be using 2.0 on another (aix) server.

Can one get a more detailed error back than IOError?  I'm counting
lines in files that may or may not exist, and if one doesn't exist, I
can set the number of lines to zero and continue with the rest.  But,
if the file can't be opened because of any other problem, such as a
permission error, I want it to bail with whatever error message is
appropriate (preferably without handling each and every case.)

def countlines(filename):
		f = open(filename, 'r')
		text = f.readlines()
		return len(text)
	except (file doesn't exist):
		return 0

Many thanks from a first instar pythonista...
Allyn Weaks    allyn@tardigrade.net   Seattle, WA  Sunset zone 5
Pacific NW Native Wildlife Gardening: http://www.tardigrade.org/natives/
"The benefit of even limited monopolies is too doubtful, to be opposed
to that of their general suppression."  Thomas Jefferson