Re: Safely renaming a file without overwriting

2006-10-29 Thread Wolfgang Draxinger
Diez B. Roggisch wrote: > The link/unlink trick of Chetan sounds reasonable, though. It will work only if both source and destination are on the same file system, which means you can't move a file between mount points - at least that's the way how it's defined by POSIX; NTFS supports hard links a

Re: Safely renaming a file without overwriting

2006-10-28 Thread Diez B. Roggisch
Wolfgang Draxinger schrieb: > Steven D'Aprano wrote: > >> But the source file always exists, otherwise there is nothing >> to rename! Do you mean, open the destination filename? > > Of course I meant the destination file. Someone please spill some > ice chilled water over me to get me awake again

Re: Safely renaming a file without overwriting

2006-10-28 Thread Wolfgang Draxinger
Steven D'Aprano wrote: > But the source file always exists, otherwise there is nothing > to rename! Do you mean, open the destination filename? Of course I meant the destination file. Someone please spill some ice chilled water over me to get me awake again. Time to go to bed :-P before I make mo

Re: Safely renaming a file without overwriting

2006-10-28 Thread Steven D'Aprano
On Sun, 29 Oct 2006 00:29:06 +0200, Wolfgang Draxinger wrote: > Steven D'Aprano wrote: > >> Open "the" file? There are potentially two files -- the source >> and destination. I only want to do the rename if the >> destination *doesn't* exist, so there is no destination file to >> open. How will i

Re: Safely renaming a file without overwriting

2006-10-28 Thread Wolfgang Draxinger
Steven D'Aprano wrote: > Open "the" file? There are potentially two files -- the source > and destination. I only want to do the rename if the > destination *doesn't* exist, so there is no destination file to > open. How will it help me to lock the source file? Have I > misunderstood? I forgot to

Re: Safely renaming a file without overwriting

2006-10-28 Thread Steven D'Aprano
On Sat, 28 Oct 2006 16:48:37 +0200, Diez B. Roggisch wrote: > Where does that help for new files? The OP was right in assuming that a > race condition could occur when he tests for a file & then tries to > create it, as in the meantime it could have been created. Ah! "Race condition" -- that wa

Re: Safely renaming a file without overwriting

2006-10-28 Thread Steven D'Aprano
On Sat, 28 Oct 2006 13:38:14 +0200, Wolfgang Draxinger wrote: >> But on a multi-user system, it is possible that dest is created >> in the time period between checking if it exists and attempting >> the rename. >> >> Is there any way to prevent this? Or do I just try to keep the >> check and the

Re: Safely renaming a file without overwriting

2006-10-28 Thread Chetan
Steven D'Aprano <[EMAIL PROTECTED]> writes: > I want to rename a file, but only if the destination file name doesn't > already exist. > > I can do this: > > if os.path.exists(dest): > # skip file, raise an exception, make a backup... > do_something_else() > else: > os.rename(src, de

Re: Safely renaming a file without overwriting

2006-10-28 Thread Wolfgang Draxinger
Diez B. Roggisch wrote: >> 1: Open the file with os.open >> >> 2: Lock the file exclusively -> no other process can now >> access it. >> >> 3: Use rename to rename the file; this causes a file system >> level implicit unlink of the old file (it dissappears from the >> file system) but the openin

Re: Safely renaming a file without overwriting

2006-10-28 Thread Diez B. Roggisch
Wolfgang Draxinger schrieb: > Steven D'Aprano wrote: > >> I want to rename a file, but only if the destination file name >> doesn't already exist. >> >> I can do this: >> >> if os.path.exists(dest): >> # skip file, raise an exception, make a backup... >> do_something_else() >> else: >>

Re: Safely renaming a file without overwriting

2006-10-28 Thread Wolfgang Draxinger
Steven D'Aprano wrote: > I want to rename a file, but only if the destination file name > doesn't already exist. > > I can do this: > > if os.path.exists(dest): > # skip file, raise an exception, make a backup... > do_something_else() > else: > os.rename(src, dest) > > > But on a m

Safely renaming a file without overwriting

2006-10-28 Thread Steven D'Aprano
I want to rename a file, but only if the destination file name doesn't already exist. I can do this: if os.path.exists(dest): # skip file, raise an exception, make a backup... do_something_else() else: os.rename(src, dest) But on a multi-user system, it is possible that dest is cre