I suggest

try:
  f=open(fname,'rb').read()
except IOError:
  ... error message ...

it is as robust as you suggest but more explicit. There is nothing
wrong with try ... except ... if you know what to except.

On 4 Lug, 15:33, weheh <[email protected]> wrote:
> Can anyone comment as to whether this is good style:
>
> try:
>   f=open(fname,'rb').read()
> except:
>   ... error message ...
>
> ------- vs. ---------
>
> import os.path
> if os.path.isfile(fname):
>   f=open(fname,'rb').read()
> else:
>   ... error message ...
>
> I much prefer the first example because it's so much easier and quite
> robust. I figure I still have to do the try when opening the file,
> even in the 2nd case. Am I paying a performance penalty for having my
> cake and eating it?

Reply via email to