Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; ';-)': 0.03; 'mrab': 0.05; 'output': 0.05; 'finally:': 0.07; 'try:': 0.09; 'yeah,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; '"w")': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'func():': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; '31,': 0.24; "shouldn't": 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'file': 0.32; 'stuff': 0.32; 'open': 0.33; 'fri,': 0.33; "can't": 0.35; 'done.': 0.35; 'but': 0.35; 'received:google.com': 0.35; "you've": 0.63; 'believe': 0.68; "'with'": 0.84; 'mistake': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=BP47UOO88SLksrLs6XafVSbQIYHBMWDqWckMVPwlzew=; b=Au0L5hww7P6gl15DccXCyWzfFLeX8zETx+FihyKTKI8E82KC5m7o8RaI+m1fPGn8cn +ftR2sjwU7grnUJ9ClxPv35ExXLsle/EuW6tvvZd7VQkXWIfBecHH+qs+2W4EjLS0yqq c3nbMknH3uQRBdpLph1v82lFIfdSGWqBlkxnmian5ojZWmyiKx08F2UzGLtPZURbguQB nrcLJo9A2cDZppFUXVcpM8ue2hcrE+ZY7NQ3Z7E/JObZi9Fn8W9YriEnB4v6C4vLS7gh PwhPynPNwJYlD7+CXOpXegHsPHdnDeaNq7pq8nR3poObek2CE1hwXQ9zuFSZsgI+Aq57 oGDg== MIME-Version: 1.0 X-Received: by 10.66.118.71 with SMTP id kk7mr14307017pab.14.1391087967349; Thu, 30 Jan 2014 05:19:27 -0800 (PST) In-Reply-To: <52EA4F98.2050509@mrabarnett.plus.com> References: <9314ac52-a2be-4382-94ef-2c291f32be1a@googlegroups.com> <52EA4F98.2050509@mrabarnett.plus.com> Date: Fri, 31 Jan 2014 00:19:27 +1100 Subject: Re: Try-except-finally paradox From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 18 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391087975 news.xs4all.nl 2968 [2001:888:2000:d::a6]:33771 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64994 On Fri, Jan 31, 2014 at 12:11 AM, MRAB 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