Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.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; 'else:': 0.03; 'from:addr:yahoo.co.uk': 0.04; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'type,': 0.09; 'windows,': 0.09; 'language.': 0.14; '6:00': 0.16; 'blocks:': 0.16; 'ioerror:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'language': 0.16; 'wrote:': 0.18; 'written': 0.21; 'seems': 0.21; 'example': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'instead.': 0.24; 'mon,': 0.24; '---': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply- To:1': 0.27; 'chris': 0.29; 'code': 0.31; 'lines': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'trivial': 0.31; 'file': 0.32; 'except': 0.35; 'surely': 0.36; 'two': 0.37; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'free': 0.61; 'break': 0.61; 'viruses': 0.61; 'name': 0.63; 'protection': 0.63; 'our': 0.64; 'antivirus': 0.68; 'jul': 0.74; 'life.': 0.83 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: open() and EOFError Date: Mon, 07 Jul 2014 14:06:33 +0100 References: <53ba11fc$0$29985$c3e8da3$5496439d@news.astraweb.com> <53ba538d$0$2926$c3e8da3$76491128@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-92-18-5-57.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: X-Antivirus: avast! (VPS 140707-0, 07/07/2014), Outbound message X-Antivirus-Status: Clean 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404738409 news.xs4all.nl 2947 [2001:888:2000:d::a6]:38733 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74104 On 07/07/2014 09:09, Chris Angelico wrote: > On Mon, Jul 7, 2014 at 6:00 PM, Steven D'Aprano wrote: >> How do people feel about code like this? >> >> try: >> name = input("Enter file name, or Ctrl-D to exit") >> # On Windows, use Ctrl-Z [enter] instead. >> fp = open(name) >> except EOFError: >> sys.exit() >> except IOError: >> handle_bad_file(name) >> else: >> handle_good_file(fp) > > It seems trivial in this example to break it into two try blocks: > > try: > name = input("Enter file name, or Ctrl-D to exit") > # On Windows, use Ctrl-Z [enter] instead. > except EOFError: > sys.exit() > try: > fp = open(name) > except IOError: > handle_bad_file(name) > else: > handle_good_file(fp) > All those extra lines to type, not on your life. Surely it would be better written as a one liner? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com