Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68277

Re: Save to a file, but avoid overwriting an existing file

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 <skip.montanaro@gmail.com>
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 <lfpjv9$tki$1@news1.carnet.hr>
References <lfpjv9$tki$1@news1.carnet.hr>
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 <skip@pobox.com>
To zoom <zoom@yahoo.com>
Content-Type text/plain; charset=UTF-8
Cc Python <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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8086.1394627874.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Save to a file, but avoid overwriting an existing file zoom <zoom@yahoo.com> - 2014-03-12 13:29 +0100
  Re: Save to a file, but avoid overwriting an existing file Skip Montanaro <skip@pobox.com> - 2014-03-12 07:37 -0500
  Re: Save to a file, but avoid overwriting an existing file Tim Chase <python.list@tim.thechases.com> - 2014-03-12 08:33 -0500
  Re:Save to a file, but avoid overwriting an existing file Dave Angel <davea@davea.name> - 2014-03-12 14:22 -0400
  Re: Save to a file, but avoid overwriting an existing file Emile van Sebille <emile@fenx.com> - 2014-03-12 12:38 -0700
  Re: Save to a file, but avoid overwriting an existing file Cameron Simpson <cs@zip.com.au> - 2014-03-13 09:19 +1100
  Re: Save to a file, but avoid overwriting an existing file Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-12 23:04 +0000
  Re: Save to a file, but avoid overwriting an existing file Ben Finney <ben+python@benfinney.id.au> - 2014-03-13 11:22 +1100

csiph-web