Re: Rename file without overwriting existing files

2017-02-12 Thread eryk sun
On Sun, Feb 12, 2017 at 4:09 AM, Steve D'Aprano wrote: > On Fri, 10 Feb 2017 12:07 am, eryk sun wrote: > >> On Thu, Feb 9, 2017 at 11:46 AM, Steve D'Aprano >> wrote: >>> >>> So to summarise, os.rename(source, destination): >>> >>> - is

Re: Rename file without overwriting existing files

2017-02-11 Thread Steve D'Aprano
On Fri, 10 Feb 2017 12:07 am, eryk sun wrote: > On Thu, Feb 9, 2017 at 11:46 AM, Steve D'Aprano > wrote: >> >> So to summarise, os.rename(source, destination): >> >> - is atomic on POSIX systems, if source and destination are both on the >> same file system; >> -

Re: Rename file without overwriting existing files

2017-02-09 Thread eryk sun
On Thu, Feb 9, 2017 at 11:46 AM, Steve D'Aprano wrote: > > So to summarise, os.rename(source, destination): > > - is atomic on POSIX systems, if source and destination are both on the > same file system; > - may not be atomic on Windows? > - may over-write an

Re: Rename file without overwriting existing files

2017-02-09 Thread Jussi Piitulainen
Steve D'Aprano writes: > On Mon, 30 Jan 2017 09:39 pm, Peter Otten wrote: > > def rename(source, dest): >> ... os.link(source, dest) >> ... os.unlink(source) >> ... > rename("foo", "baz") > os.listdir() >> ['bar', 'baz'] > rename("bar", "baz") >> Traceback (most recent

Re: Rename file without overwriting existing files

2017-02-09 Thread Steve D'Aprano
On Tue, 31 Jan 2017 11:17 am, Ben Finney wrote: > Peter Otten <__pete...@web.de> writes: > >> http://stackoverflow.com/questions/3222341/how-to-rename-without-race-conditions >> >> and from a quick test it appears to work on Linux: > > By “works on Linux”, I assume you mean “works on

Re: Rename file without overwriting existing files

2017-02-09 Thread Steve D'Aprano
On Mon, 30 Jan 2017 09:39 pm, Peter Otten wrote: def rename(source, dest): > ... os.link(source, dest) > ... os.unlink(source) > ... rename("foo", "baz") os.listdir() > ['bar', 'baz'] rename("bar", "baz") > Traceback (most recent call last): > File "", line 1, in >

Re: Rename file without overwriting existing files

2017-02-01 Thread Steve D'Aprano
On Tue, 31 Jan 2017 02:56 am, Grant Edwards wrote: > On 2017-01-30, Terry Reedy wrote: >> On 1/30/2017 8:58 AM, Peter Otten wrote: >>> Jussi Piitulainen wrote: >> It doesn't seem to be documented. >>> >>> For functions with a C equivalent a look into the man page is

Re: Rename file without overwriting existing files

2017-01-30 Thread Ben Finney
Peter Otten <__pete...@web.de> writes: > http://stackoverflow.com/questions/3222341/how-to-rename-without-race-conditions > > and from a quick test it appears to work on Linux: By “works on Linux”, I assume you mean “works on filesystems that use inodes and hard links”. That is not true for all

Re: Rename file without overwriting existing files

2017-01-30 Thread Marko Rauhamaa
Grant Edwards : > IMO, beginners shouldn't be using the os module. Hard to know. Depends on what the beginner wants to accomplish. > I always found the first sentence to be a bit funny: > > This module provides a portable way of using operating system >

Re: Rename file without overwriting existing files

2017-01-30 Thread Grant Edwards
On 2017-01-30, Terry Reedy wrote: > On 1/30/2017 8:58 AM, Peter Otten wrote: >> Jussi Piitulainen wrote: > >>> It doesn't seem to be documented. >> >> For functions with a C equivalent a look into the man page is usually >> helpful. > > Man pages do not exist on Windows. I

Re: Rename file without overwriting existing files

2017-01-30 Thread Grant Edwards
On 2017-01-30, Jussi Piitulainen wrote: > It doesn't seem to be documented. I looked at help(os.link) on Python > 3.4 and the corresponding current library documentation on the web. I > saw no mention of what happens when dst exists already. The functions in the

Re: Rename file without overwriting existing files

2017-01-30 Thread Jon Ribbens
On 2017-01-30, Peter Otten <__pete...@web.de> wrote: > Jon Ribbens wrote: >> On 2017-01-30, Peter Otten <__pete...@web.de> wrote: >>> However, the current Python version of link() is sufficiently different >>> from >>>, say, to warrant its own documentation. >>

Re: Rename file without overwriting existing files

2017-01-30 Thread Peter Otten
Chris Angelico wrote: > On Tue, Jan 31, 2017 at 12:58 AM, Peter Otten <__pete...@web.de> wrote: >>> I looked at help(os.link) on Python >>> 3.4 and the corresponding current library documentation on the web. I >>> saw no mention of what happens when dst exists already. >>> >>> Also, creating a

Re: Rename file without overwriting existing files

2017-01-30 Thread Terry Reedy
On 1/30/2017 8:58 AM, Peter Otten wrote: Jussi Piitulainen wrote: It doesn't seem to be documented. For functions with a C equivalent a look into the man page is usually helpful. Man pages do not exist on Windows. I suspect that there are more individual Python programs on Windows than

Re: Rename file without overwriting existing files

2017-01-30 Thread Jussi Piitulainen
Peter Otten writes: > Jussi Piitulainen wrote: > >> Peter Otten writes: >> >>> Steve D'Aprano wrote: > The wider context is that I'm taking from 1 to path names to existing files as arguments, and for each path name I transfer the file name part (but not the directory part) and

Re: Rename file without overwriting existing files

2017-01-30 Thread Peter Otten
Jon Ribbens wrote: > On 2017-01-30, Peter Otten <__pete...@web.de> wrote: >> Jon Ribbens wrote: >>> A lot of the functions of the 'os' module do nothing but call the >>> underlying OS system call with the same name. It would not only be >>> redundant to copy the OS documentation into the Python

Re: Rename file without overwriting existing files

2017-01-30 Thread Chris Angelico
On Tue, Jan 31, 2017 at 12:58 AM, Peter Otten <__pete...@web.de> wrote: >> I looked at help(os.link) on Python >> 3.4 and the corresponding current library documentation on the web. I >> saw no mention of what happens when dst exists already. >> >> Also, creating a hard link doesn't seem to work

Re: Rename file without overwriting existing files

2017-01-30 Thread Jon Ribbens
On 2017-01-30, Peter Otten <__pete...@web.de> wrote: > Jon Ribbens wrote: >> A lot of the functions of the 'os' module do nothing but call the >> underlying OS system call with the same name. It would not only be >> redundant to copy the OS documentation into the Python documentation, >> it would

Re: Rename file without overwriting existing files

2017-01-30 Thread Peter Otten
Jussi Piitulainen wrote: > Peter Otten writes: > >> Steve D'Aprano wrote: >>> The wider context is that I'm taking from 1 to >>> path names to existing files as arguments, and for each path name I >>> transfer the file name part (but not the directory part) and then rename >>> the file. For

Re: Rename file without overwriting existing files

2017-01-30 Thread Peter Otten
Jon Ribbens wrote: > On 2017-01-30, Jussi Piitulainen wrote: >> It doesn't seem to be documented. I looked at help(os.link) on Python >> 3.4 and the corresponding current library documentation on the web. I >> saw no mention of what happens when dst exists already.

Re: Rename file without overwriting existing files

2017-01-30 Thread Jon Ribbens
On 2017-01-30, Jussi Piitulainen wrote: > It doesn't seem to be documented. I looked at help(os.link) on Python > 3.4 and the corresponding current library documentation on the web. I > saw no mention of what happens when dst exists already. > > Also, creating a

Re: Rename file without overwriting existing files

2017-01-30 Thread Wolfgang Maier
On 01/30/2017 03:49 AM, Steve D'Aprano wrote: This code contains a Time Of Check to Time Of Use bug: if os.path.exists(destination) raise ValueError('destination already exists') os.rename(oldname, destination) In the microsecond between checking for the existence of the

Re: Rename file without overwriting existing files

2017-01-30 Thread Jussi Piitulainen
Peter Otten writes: > Steve D'Aprano wrote: > >> On Mon, 30 Jan 2017 03:33 pm, Cameron Simpson wrote: >> >>> On 30Jan2017 13:49, Steve D'Aprano wrote: This code contains a Time Of Check to Time Of Use bug: if os.path.exists(destination)

Re: Rename file without overwriting existing files

2017-01-30 Thread Peter Otten
Steve D'Aprano wrote: > On Mon, 30 Jan 2017 03:33 pm, Cameron Simpson wrote: > >> On 30Jan2017 13:49, Steve D'Aprano wrote: >>>This code contains a Time Of Check to Time Of Use bug: >>> >>>if os.path.exists(destination) >>>raise ValueError('destination

Re: Rename file without overwriting existing files

2017-01-30 Thread Steve D'Aprano
On Mon, 30 Jan 2017 03:33 pm, Cameron Simpson wrote: > On 30Jan2017 13:49, Steve D'Aprano wrote: >>This code contains a Time Of Check to Time Of Use bug: >> >>if os.path.exists(destination) >>raise ValueError('destination already exists') >>

Re: Rename file without overwriting existing files

2017-01-29 Thread Cameron Simpson
On 30Jan2017 13:49, Steve D'Aprano wrote: This code contains a Time Of Check to Time Of Use bug: if os.path.exists(destination) raise ValueError('destination already exists') os.rename(oldname, destination) In the microsecond between checking for the

Re: Rename file without overwriting existing files

2017-01-29 Thread MRAB
On 2017-01-30 03:27, Chris Angelico wrote: On Mon, Jan 30, 2017 at 1:49 PM, Steve D'Aprano wrote: This code contains a Time Of Check to Time Of Use bug: if os.path.exists(destination) raise ValueError('destination already exists') os.rename(oldname,

Re: Rename file without overwriting existing files

2017-01-29 Thread Chris Angelico
On Mon, Jan 30, 2017 at 1:49 PM, Steve D'Aprano wrote: > This code contains a Time Of Check to Time Of Use bug: > > if os.path.exists(destination) > raise ValueError('destination already exists') > os.rename(oldname, destination) > > > In the

Rename file without overwriting existing files

2017-01-29 Thread Steve D'Aprano
This code contains a Time Of Check to Time Of Use bug: if os.path.exists(destination) raise ValueError('destination already exists') os.rename(oldname, destination) In the microsecond between checking for the existence of the destination and actually doing the rename, it is