Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'lawrence': 0.09; 'try:': 0.09; 'type,': 0.09; 'windows,': 0.09; 'cc:addr :python-list': 0.11; '6:00': 0.16; 'blocks:': 0.16; 'ioerror:': 0.16; 'wrote:': 0.18; 'written': 0.21; 'seems': 0.21; '>>>': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; 'this?': 0.23; 'instead.': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'lines': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'trivial': 0.31; 'file': 0.32; 'except': 0.35; 'received:google.com': 0.35; 'surely': 0.36; 'two': 0.37; 'pm,': 0.38; 'how': 0.40; 'break': 0.61; 'name': 0.63; 'afraid': 0.65; 'jul': 0.74; 'life.': 0.83; 'to:addr:yahoo.co.uk': 0.84 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:to :cc:content-type; bh=zbQ5Dj37i/8TKFstMYARMfFd5CARAzCScKnH3p2Fjm4=; b=gxCca+ELFCBkvWml781ioYms07miUIiOvil0v8sinVSYnFT2TOqLpuQjMfWil6HMj4 UUic6ujeJ9vs0UscVv06jdGxzlas8CqWWhnNx/0RnBFP2sCbPq1wIY6F/hk1V4sURSPO TJooAqAetaguOSEwlJ9LhUx02c8Ro4/OAQqtEl5zaMQYyYOrDsLsaIm3w+9cvur6AeCR /3oVLKWUd2T+8WN1EonLUXlMYocIMrTVeDz2dURitL29X+OI3WLYu74Qck8h3j5yuZMf aGDylfnP73fxD0fc30Zp2OV/YlnbF4M/QyKVHvYy3Y/oxJbfqxhpdXnYCX30o4Ql1NkP ZbZw== MIME-Version: 1.0 X-Received: by 10.220.99.66 with SMTP id t2mr13650313vcn.40.1404749263874; Mon, 07 Jul 2014 09:07:43 -0700 (PDT) In-Reply-To: References: <53ba11fc$0$29985$c3e8da3$5496439d@news.astraweb.com> <53ba538d$0$2926$c3e8da3$76491128@news.astraweb.com> Date: Mon, 7 Jul 2014 09:07:43 -0700 Subject: Re: open() and EOFError From: Dan Stromberg To: Mark Lawrence Content-Type: text/plain; charset=UTF-8 Cc: python-list@python.org 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404749266 news.xs4all.nl 2915 [2001:888:2000:d::a6]:44924 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74120 On 7/7/14, Mark Lawrence wrote: > 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? Don't be afraid of a few extra keystrokes. Clarity is king.