Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(at': 0.04; 'e.g.,': 0.09; 'lawrence': 0.09; 'pep': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'windows': 0.15; 'cleaner': 0.16; 'command.': 0.16; 'eof': 0.16; 'finer': 0.16; 'guarded': 0.16; 'ioerror:': 0.16; 'open()': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'bit': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'handling': 0.26; 'least': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'ignored.': 0.30; 'usually': 0.31; 'exceptions': 0.31; 'file': 0.32; 'another': 0.32; 'maybe': 0.34; 'could': 0.34; 'except': 0.35; 'but': 0.35; 'revert': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'providing': 0.61; 'within': 0.65; 'home.': 0.72 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Neil D. Cerutti" Subject: Re: open() and EOFError Date: Tue, 08 Jul 2014 10:29:18 -0400 References: <53ba11fc$0$29985$c3e8da3$5496439d@news.astraweb.com> <53ba538d$0$2926$c3e8da3$76491128@news.astraweb.com> <87wqbp9it2.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: jackman.norwich.edu User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404829781 news.xs4all.nl 2929 [2001:888:2000:d::a6]:36598 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74167 On 7/7/2014 7:10 PM, Mark Lawrence wrote: > On 07/07/2014 23:09, Gregory Ewing wrote: >> Marko Rauhamaa wrote: >>> with open(path) as f: >>> ... >>> >>> If the open() call is guarded against exceptions (as it usually should), >>> one must revert to the classic syntax: >> >> Hmmm, maybe we could do with a with-except statement: >> >> with open(path) as f: >> ... >> except IOError: >> # Catches exceptions in the with-expression only >> ... >> >> Although that would be a bit confusing. > > I wrap the with inside a try/except, the other file handling parts > within another try/except and use the finer grained exceptions from PEP > 3151 to write (at least to my eye) cleaner looking code. Somehow I > think we'll get agreement on the best way to do this when the cows come > home. On Windows it's my experience that EOF from interactive sessions is ignored. Programs keep going as best they can, providing some other means of exit, e.g., an 'exit' command. But maybe that's just the shell. -- Neil Cerutti