basic python questions
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Sun Nov 19 17:57:28 EST 2006
Diez B. Roggisch a écrit :
> nateastle at gmail.com schrieb:
>
>> I have taken the coments and think I have implemented most. My only
>
>
> Unfortunately, no.
>
>> question is how to use the enumerator. Here is what I did, I have tried
>> a couple of things but was unable to figure out how to get the line
>> number.
>>
>> def Xref(filename):
>> try:
>> fp = open(filename, "r")
>> except:
>> raise "Couldn't read input file \"%s\"" % filename
>
>
> You still got that I-catch-all-except in there.
> This will produce subtle bugs when you e.g. misspell a variable name:
>
> filename = '/tmp/foo'
> try:
> f = open(fliename, 'r')
> except:
> raise "can't open filename"
>
>
> Please notice the wrong-spelled 'fliename'.
>
> This OTOH will give you more clues on what really goes wrong:
>
>
>
> filename = '/tmp/foo'
> try:
> f = open(fliename, 'r')
> except IOError:
> raise "can't open filename"
>
>
And this would be still more informative (and not deprecated...):
filename = '/tmp/foo'
f = open(fliename)
Catching an exception just to raise a less informative one is somewhat
useless IMHO.
More information about the Python-list
mailing list