Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #68290
| From | Dave Angel <davea@davea.name> |
|---|---|
| Subject | Re:Save to a file, but avoid overwriting an existing file |
| Date | 2014-03-12 14:22 -0400 |
| Organization | news.gmane.org |
| References | <lfpjv9$tki$1@news1.carnet.hr> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.8097.1394648316.18130.python-list@python.org> (permalink) |
zoom <zoom@yahoo.com> Wrote in message:
> Hi!
>
> 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.
>
The tempfile module is your best answer, but if you really need
to keep the file afterwards, you'll have the same problem when
you rename it later.
I suggest you learn about try/except. For simple cases it's not
that tough, though if you want to ask about it, you'll need to
specify your Python version.
--
DaveA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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