Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'exercise': 0.04; 'subject:file': 0.07; 'decision.': 0.09; 'fname': 0.09; 'cc:addr :python-list': 0.11; '"w")': 0.16; '"x"': 0.16; 'exists,': 0.16; 'from:addr:pobox.com': 0.16; 'from:addr:skip': 0.16; 'naming': 0.16; 'sender:addr:gmail.com': 0.17; 'seems': 0.21; 'example': 0.22; 'cc:addr:python.org': 0.22; 'skip': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'leave': 0.29; 'message-id:@mail.gmail.com': 0.30; 'workaround': 0.31; 'file': 0.32; 'problem': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'so,': 0.37; 'implement': 0.38; 'skip:o 20': 0.38; 'solving': 0.38; 'whatever': 0.38; 'rather': 0.38; 'simple,': 0.60; 'choose': 0.64; 'safe': 0.72; 'to:addr:yahoo.com': 0.81; 'subject:Save': 0.91; 'race': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=IsH282s0zD3HrUKbPsm4CsUQ+LYYW/qR02N4PRiGLLQ=; b=cFAQhV1AyK3B4pkyhyBPYFtUT53qeIa4hShhbiT+Ei14qpOf/rZWisxRXd1SPo3kLT HUaey0TnN5WIJDvrVYpVm0M7o+L5Gl1kL/7zG13OjWot1RREKcnC7EucB8qA+/HbIRCF mO9CqmhNxJp3mwtLoMQAm8D5ePbJ+WTmmDEmPUSkxHOaMu9B3rPdHYvHnmbP5ZE/Tvz1 j6fWfpYwAkDyBhMavBbZpIosi9rz5QEkA7WNrVWY9rLKH/zGLPdl19DeFxDdYzRqDBYy uenNUzo1K0Oj6rbKPvUg0Hf0dw+tfit9w1t/QJOHdjFWTICMKwZhJ5KXRxAGfxVnZc0D Iyyw== MIME-Version: 1.0 X-Received: by 10.50.47.231 with SMTP id g7mr15712371ign.8.1394627864885; Wed, 12 Mar 2014 05:37:44 -0700 (PDT) Sender: skip.montanaro@gmail.com In-Reply-To: References: Date: Wed, 12 Mar 2014 07:37:44 -0500 X-Google-Sender-Auth: bQITUNmAiuf45943z25Pz-KMv3Q Subject: Re: Save to a file, but avoid overwriting an existing file From: Skip Montanaro To: zoom Content-Type: text/plain; charset=UTF-8 Cc: Python 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: 14 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394627874 news.xs4all.nl 2883 [2001:888:2000:d::a6]:38935 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68277 This seems to be an application-level decision. If so, in your application, why not just check to see if the file exists, and implement whatever workaround you deem correct for your needs? For example (to choose a simple, but rather silly, file naming strategy): fname = "x" while os.path.exists(fname): fname = "%s.%f" % (fname, random.random()) fd = open(fname, "w") It's clearly not going to be safe from race conditions, but I leave solving that problem as an exercise for the reader. Skip