Victor Hooi <victorh...@gmail.com> writes:

>     try:
>         with open('somefile.log', 'wb' as f:
>             f.write("hello there")
>     except IOError as e:
>         logger.error("Uhoh, the file wasn't there").

IOError, as Steven D'Aprano points out, is not equivalent to “file not
found”. Also, you're not doing anything with the exception object, so
there's no point binding it to the name ‘e’.

What you want is the specific FileNotFoundError:

    try:
        with open('somefile.log', 'wb' as f:
            f.write("hello there")
    except FileNotFoundError:
        logger.error("Uhoh, the file wasn't there").

See <URL:http://docs.python.org/3/library/exceptions.html#FileNotFoundError>.

-- 
 \            “Choose mnemonic identifiers. If you can't remember what |
  `\                mnemonic means, you've got a problem.” —Larry Wall |
_o__)                                                                  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to