Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!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.046 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'api.': 0.05; 'subject:file': 0.07; 'messing': 0.09; 'thus,': 0.09; 'python': 0.11; 'doing,': 0.16; 'errno': 0.16; 'exception;': 0.16; 'finney': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'imo,': 0.16; 'message- id:@cskk.homeip.net': 0.16; 'readable': 0.16; 'recognise': 0.16; 'simpson': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'header:User- Agent:1': 0.23; 'error': 0.23; 'documented': 0.24; 'finally,': 0.24; 'instance,': 0.24; 'precise': 0.24; 'simpler': 0.24; 'cheers,': 0.24; 'performing': 0.26; 'header:In-Reply-To:1': 0.27; 'mode': 0.30; 'catching': 0.31; 'grouping': 0.31; 'values.': 0.31; 'writes:': 0.31; 'class': 0.32; "we're": 0.32; 'another': 0.32; 'url:python': 0.33; 'test': 0.35; 'but': 0.35; 'interface,': 0.36; 'url:org': 0.36; 'actions': 0.38; 'ben': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; '8bit%:29': 0.60; 'catch': 0.60; 'new': 0.61; 'no.': 0.61; 'url:3': 0.61; 'content- disposition:inline': 0.62; 'more': 0.64; 'different': 0.65; 'road': 0.65; 'yes': 0.68; 'importantly,': 0.68; 'received:113': 0.68; 'received:net.au': 0.68; 'therefore': 0.72; 'received:203': 0.74; 'bare': 0.84; 'metal': 0.84; 'traveled': 0.84; 'received:(may be forged)': 0.91; 'subject:Save': 0.91; 'responses': 0.93 Date: Thu, 13 Mar 2014 13:49:27 +1100 From: Cameron Simpson To: python-list@python.org Subject: Re: Save to a file, but avoid overwriting an existing file MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <85zjkvrlpc.fsf@benfinney.id.au> User-Agent: Mutt/1.5.21 (2010-09-15) References: <85zjkvrlpc.fsf@benfinney.id.au> 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: 1394678982 news.xs4all.nl 2906 [2001:888:2000:d::a6]:58749 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68321 On 13Mar2014 11:22, Ben Finney wrote: > Cameron Simpson writes: > > Therefore you need to continue _only_ if you get EEXIST. Otherwise > > abort. > > If you target Python 3.3 or later, you can catch “FileExistsError” > > which is far simpler than messing around with ‘errno’ values. Yes and no. Firstly, we Old Farts find these new ways confusing:-) Secondly, and perhaps more importantly, if I were using the O_EXCL et al constants in a call to os.open as the OP is doing, I would want to be equally picky about what I recognise in the exception; I _want_ to use error explicitly. In particular, the behaviour of "os.open" is pretty much a bare metal call to the POSIX "open(2)" OS interface, and so I want my response to be framed directly in terms of the documented error responses for that API. Thus, errno values. Finally, if I were grouping my responses to the exception by type and perhaps performing a few different actions depending on the error, then the new class hierachy is a good way to do it. But for this instance, IMO, we're catching exactly one failure mode as "retry with another name" and reraising anything else. So I don't find an errno test any less readable that a class comparison, and it still feels more precise to me. Cheers, -- Cameron Simpson The road less traveled is FASTER !! - Luc Marcouiller, marcouil@ireq.hydro.qc.ca