Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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; 'instance.': 0.09; 'try:': 0.09; 'windows,': 0.09; 'cc:addr:python-list': 0.11; '6:00': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ioerror,': 0.16; 'ioerror:': 0.16; 'wrote:': 0.18; "hasn't": 0.19; 'split': 0.19; 'cc:addr:python.org': 0.22; 'this?': 0.23; 'instead.': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'file': 0.32; "i'd": 0.34; 'except': 0.35; 'case,': 0.35; 'received:google.com': 0.35; 'possible': 0.36; 'pm,': 0.38; 'previous': 0.38; 'how': 0.40; 'dave': 0.60; 'name': 0.63; 'jul': 0.74; '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=KVLeFnWAexsjSxruyDcg0m7aF0mYJbGwGsa4c5NQHjE=; b=Bko1FNAv7gmvqaDum22sRvGsjfPsgC+b09X6eKxMddA/+tbyF+hmlM4iVAqP1zYNXD FcX6J7xCJk1kjztl6exRXL8GVbNcwf6YSD4L6mq9GDa/TkVyoO3MCNEkTeC/nsIfl+6i AIogwBJKQVJAFFg3CyJezKKSCU9yvZHWLqdfOiXqYY+EW7hn/VBcjMs4B94jt24qYxO/ pfKpMA728nnOPNoRPWkFNKBmP4XdHg0XTGdJzsF/OepEa4otTQhSUuu9Z8dS2CV2C2qB jnJ95B1+VdBK9ebn2sOu0nLBINignHmYoznEr6EoT/NB/pzBucvRyYcDQ5/CmEJtPIAv cVqQ== MIME-Version: 1.0 X-Received: by 10.58.65.40 with SMTP id u8mr1411451ves.53.1404735560945; Mon, 07 Jul 2014 05:19:20 -0700 (PDT) In-Reply-To: <53ba538d$0$2926$c3e8da3$76491128@news.astraweb.com> References: <53ba11fc$0$29985$c3e8da3$5496439d@news.astraweb.com> <53ba538d$0$2926$c3e8da3$76491128@news.astraweb.com> Date: Mon, 7 Jul 2014 22:19:20 +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: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404735570 news.xs4all.nl 2897 [2001:888:2000:d::a6]:51246 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74099 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) Just thought of something. It's possible for input() to raise IOError, if I'm not mistaken; consider redirection, for instance. In that case, you would definitely want to split the try blocks, so you don't try to "handle_bad_file" when no name was entered. (As Dave says, name hasn't been assigned at that point, although I'd be less concerned about a possible cascaded NameError than about a possible handle_bad_file() with the previous name, if this is in a loop.) ChrisA