[Tutor] IOError exception handling

lumbricus@gmx.net lumbricus@gmx.net
Mon, 29 Jul 2002 11:41:54 +0200 (MEST)


> On 28/7/02, Danny Yoo wrote:
> "Sean 'Shaleh' Perry" <shalehperry@attbi.com> wrote

[ snip ]

> Many thanks.  Betwixt the two of you, I've now got:
> 
> def errortest():
>     try:
>         fd = open(filename)
>         text = fd.readlines()
>         return len(text)
>     except IOError, e:
>         if e[0] == 2: return 0  # file not found
>         print e                 # any other error
>         sys.exit(1)
> 
> Which works when I force errors of several types.  Do I need the
> sys.exit(1), and is 1 a reasonable generic number?  I've seen it in
> several examples, and since I'm still partly in monkey-see, monkey-do
> mode, I stuck it in even though it superficially works without it. :-)

How is that:

print errno.errorcode[e]
sys.exit(errno)

HTH, HAND
J"o!

-- 

-- 
GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net