Rename file without overwriting existing files
jussi.piitulainen at helsinki.fi
Mon Jan 30 09:40:15 EST 2017
Peter Otten writes:
> Jussi Piitulainen wrote:
>> Peter Otten writes:
>>> Steve D'Aprano wrote:
>>>> The wider context is that I'm taking from 1 to <arbitrarily huge number>
>>>> 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 example:
>>>> may be renamed to:
>>>> but only provided ham.txt doesn't already exist.
>>> Google finds
>>> and from a quick test it appears to work on Linux:
>> It doesn't seem to be documented.
> For functions with a C equivalent a look into the man page is usually
Followed by a few test cases to see what Python actually does, at least
in those particular test cases, I suppose. Yes.
But is it a bug in Python if a Python function *doesn't* do what the
relevant man page in the user's operating system says? Or whatever the
user's documentation entry is called. For me, yes, it's a man page.
>> 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 between different file
>> systems, which may well be relevant to Steve's case.
> In his example above he operates inside a single directory. Can one
> directory spread across multiple file systems?
Hm, you are right, he does say he's working in a single directory.
But *I'm* currently working on processes where results from a batch
system are eventually moved to another directory, and I have no control
over the file systems. So while it was interesting to learn about
os.link, I cannot use os.link here; on the other hand, I can use
shutil.move, and in my present case it will only accidentally overwrite
a file if I've made a programming mistake myself, or if the underlying
platform is not working as advertised, so I'm in a different situation.
More information about the Python-list