Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed3.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'lines,': 0.07; 'lawrence': 0.09; 'try:': 0.09; 'type,': 0.09; 'windows,': 0.09; 'cc:addr:python-list': 0.11; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ioerror:': 0.16; 'wrote:': 0.18; 'written': 0.21; 'cc:addr:python.org': 0.22; 'instead.': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; 'file': 0.32; 'style': 0.33; 'except': 0.35; 'problem.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'surely': 0.36; 'pm,': 0.38; 'though,': 0.39; "you're": 0.61; 'name': 0.63; 'jul': 0.74; 'life.': 0.83; '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=1wZHjeEy0aoo/3ACKaQxLGUSSniXxciul7vNCPLWDSo=; b=iGZdpgKZ+uZOCSpM4xeNzl90Vjek4r5EtV4cU3RQVTeS6/VD5GIvVbARoeY6USuVwy ghFc5IM088ECcuyP59XVRQ1kPLjFhZLi7XjlQVlRTBsbDORc4ojaKyav3lZPJ0+q347D L8CDrsBK6KBE/nXQpePNdAHV4v8UNqvDCu1QrzWcc1dp+eWeBkfUfexSrbpGvq4dOBk6 kJslLj7zdlUTiuSzXRAvJ9YHeF3vxS9nE0uc6vbIezpQwsNf3NMI2XQ8aL8ipUZ4w/g2 Gu3JunmilOCdwjVNjljho8mvjSlRteY00A2OKfGIpsqfFGH5G2NwKTBYQRyhusVj6ful SkkA== MIME-Version: 1.0 X-Received: by 10.220.192.129 with SMTP id dq1mr757046vcb.57.1404739749153; Mon, 07 Jul 2014 06:29:09 -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 23:29:09 +1000 Subject: Re: open() and EOFError 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404739752 news.xs4all.nl 2976 [2001:888:2000:d::a6]:44924 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74106 On Mon, Jul 7, 2014 at 11:06 PM, Mark Lawrence wrote: >> 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? In all seriousness, though, if you're worried about the number of lines, it's not that big a deal to squish up the small try blocks. try: name = input("Enter file name, or Ctrl-D to exit") except EOFError: sys.exit() try: fp = open(name) except IOError: handle_bad_file(name) else: handle_good_file(fp) Might violate some style guides, but IMO it's not a problem. ChrisA