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


Groups > comp.lang.python > #68306

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'from:addr:yahoo.co.uk': 0.04; 'exists.': 0.07; 'rename': 0.07; 'subject:file': 0.07; 'exist,': 0.09; 'iterate': 0.09; 'lawrence': 0.09; 'overwrite': 0.09; 'permission,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'def': 0.12; 'language.': 0.14; '"%s.%d"': 0.16; 'descriptor.': 0.16; 'e.errno': 0.16; 'errno': 0.16; 'fails.': 0.16; 'hierarchy': 0.16; 'oserror': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'simpson': 0.16; 'task.': 0.16; 'temp': 0.16; 'true:': 0.16; 'url:whatsnew': 0.16; 'exception': 0.16; 'language': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'trying': 0.19; 'file,': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'cheers,': 0.24; "haven't": 0.24; '---': 0.24; 'script': 0.25; 'least': 0.26; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'raise': 0.29; 'see,': 0.30; "i'm": 0.30; 'code': 0.31; 'easier': 0.31; 'catching': 0.31; 'file': 0.32; 'option': 0.32; 'run': 0.32; 'open': 0.33; 'url:python': 0.33; 'used,': 0.33; 'could': 0.34; 'except': 0.35; 'problem.': 0.35; 'case,': 0.35; 'but': 0.35; 'there': 0.35; 'done': 0.36; 'url:org': 0.36; 'example,': 0.37; 'too': 0.37; 'two': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'issue': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'free': 0.61; 'new': 0.61; 'url:3': 0.61; 'viruses': 0.61; 'matter': 0.61; 'course': 0.61; 'save': 0.62; 'name': 0.63; 'protection': 0.63; 'our': 0.64; 'different': 0.65; 'antivirus': 0.68; 'therefore': 0.72; 'collision': 0.84; 'received:as9105.com': 0.84; 'received:dsl.as9105.com': 0.84; 'received:dynamic.dsl.as9105.com': 0.84; 'forever.': 0.91; 'subject:Save': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Save to a file, but avoid overwriting an existing file
Date Wed, 12 Mar 2014 23:04:13 +0000
References <lfpjv9$tki$1@news1.carnet.hr> <20140312221925.GA67514@cskk.homeip.net>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 80-44-192-255.dynamic.dsl.as9105.com
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.3.0
In-Reply-To <20140312221925.GA67514@cskk.homeip.net>
X-Antivirus avast! (VPS 140312-1, 12/03/2014), Outbound message
X-Antivirus-Status Clean
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.8111.1394665486.18130.python-list@python.org> (permalink)
Lines 79
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1394665486 news.xs4all.nl 2975 [2001:888:2000:d::a6]:48145
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:68306

Show key headers only | View raw


On 12/03/2014 22:19, Cameron Simpson wrote:
> On 12Mar2014 13:29, zoom <zoom@yahoo.com> wrote:
>> I would like to assure that when writing to a file I do not
>> overwrite an existing file, but I'm unsure which is the best way to
>> approach to this problem. As I can see, there are at least two
>> possibilities:
>>
>> 1. I could use fd = os.open("x", os.O_WRONLY | os.O_CREAT | os.O_EXCL)
>> which will fail - if the file exists. However, I would prefer if the
>> program would try to save under different name in this case, instead
>> of discarding all the calculation done until now - but I' not too
>> well with catching exceptions.
>
> Others have menthions tempfile, though of course you have the same collision
> issue when you come to rename the temp file if you are keeping it.
>
> I would run with option 1 for your task.
>
> Just iterate until os.open succeeds.
>
> However, you need to distinuish _why_ an open fails. For example,
> if you were trying to make files in a directory to which you do not
> have write permission, or just a directory that did not exist,
> os.open would fail not matter what name you used, so your loop would
> run forever.
>
> Therefore you need to continue _only_ if you get EEXIST. Otherwise abort.
>
> So you'd have some code like this (totally untested):
>
>    # at top of script
>    import errno
>
>    # where you make the file
>    def open_new(primary_name):
>      try:
>        fd = os.open(primary_name, os.O_WRONLY | os.O_CREAT | os.O_EXCL)
>      except OSError as e:
>        if e.errno != errno.EEXIST:
>          raise
>      else:
>        return primary_name, fd
>      n = 1
>      while True:
>        secondary_name = "%s.%d" % (primary_name, n)
>        try:
>          fd = os.open(secondary_name, os.O_WRONLY | os.O_CREAT | os.O_EXCL)
>        except OSError as e:
>          if e.errno != errno.EEXIST:
>            raise
>        else:
>          return secondary_name, fd
>        n += 1
>
>    # where you need the file
>    path, fd = open_new("x")
>
> That gets you a function your can reuse which returns the file's
> name and the file descriptor.
>
> Cheers,
>

I haven't looked but would things be easier if the new exception 
hierarchy were used 
http://docs.python.org/3.3/whatsnew/3.3.html#pep-3151-reworking-the-os-and-io-exception-hierarchy 
?

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

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