open() and EOFError
rosuav at gmail.com
Mon Jul 7 10:09:28 CEST 2014
On Mon, Jul 7, 2014 at 6:00 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> How do people feel about code like this?
> name = input("Enter file name, or Ctrl-D to exit")
> # On Windows, use Ctrl-Z [enter] instead.
> fp = open(name)
> except EOFError:
> except IOError:
It seems trivial in this example to break it into two try blocks:
name = input("Enter file name, or Ctrl-D to exit")
# On Windows, use Ctrl-Z [enter] instead.
fp = open(name)
But if the code's more complicated and it's not so easy to split, then
sure, doesn't seem a problem. It's like spam[foo//bar] and then
catching either IndexError or ZeroDivisionError - there's no big
confusion from having two distinct sources of two distinct errors
handled by two distinct except blocks.
More information about the Python-list