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

2014-03-12 Thread Cameron Simpson
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” > http://docs.python.org/3/library/exceptions.html#FileExistsError> > which is

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

2014-03-12 Thread Ben Finney
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” http://docs.python.org/3/library/exceptions.html#FileExistsError> which is far simpler than messing around with ‘errno’ values.

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

2014-03-12 Thread Mark Lawrence
On 12/03/2014 22:19, Cameron Simpson wrote: On 12Mar2014 13:29, zoom 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 u

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

2014-03-12 Thread Cameron Simpson
On 12Mar2014 13:29, zoom 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 |

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

2014-03-12 Thread Emile van Sebille
On 3/12/2014 5:29 AM, zoom wrote: 2. Alternatively, a unique string could be generated to assure that no same file exists. I can see one approach to this is to include date and time in the file name. But this seems to me a bit clumsy, and is not unique, i.e. it could happen (at least in theory)

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

2014-03-12 Thread Tim Chase
On 2014-03-12 13:29, zoom wrote: > 2. Alternatively, a unique string could be generated to assure that > no same file exists. I can see one approach to this is to include > date and time in the file name. But this seems to me a bit clumsy, > and is not unique, i.e. it could happen (at least in theo

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

2014-03-12 Thread Skip Montanaro
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(fn

Save to a file, but avoid overwriting an existing file

2014-03-12 Thread zoom
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 f