Try-except-finally paradox

Chris Angelico rosuav at gmail.com
Thu Jan 30 08:19:27 EST 2014


On Fri, Jan 31, 2014 at 12:11 AM, MRAB <python at mrabarnett.plus.com> wrote:
> One of the reasons that the 'with' statement was added was to prevent
> the mistake that you've just done. ;-)
>
> What if the file can't be opened?

Yeah, whoops. The open shouldn't be inside try/finally.

def func():
    output = open("output.txt", "w")
    try:
         # do a whole lot of stuff ...
    finally:
         output.close()

But my point still stands, I believe :)

ChrisA



More information about the Python-list mailing list