homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add function similar to shutil.move that does not overwrite
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: David.Townshend, eric.araujo, steven.daprano
Priority: normal Keywords:

Created on 2011年08月12日 12:41 by David.Townshend, last changed 2022年04月11日 14:57 by admin.

Messages (3)
msg141952 - (view) Author: David Townshend (David.Townshend) Date: 2011年08月12日 12:41
The shutil.move function uses os.rename to move files on the same file system. On unix, this function will overwrite an existing destination, so the obvious approach is
if not os.path.exists(dst):
 shutil.move(src, dst)
But this could result in race conditions if dst is created after os.path.exists and before shutil.move. From my research, it seems that this is a limitation in the unix c library, but it should be possible to avoid it through a workaround (pieced together from http://bytes.com/topic/python/answers/555794-safely-renaming-file-without-overwriting ). This involves some fairly low-level work, so I propose adding a new move2 function to shutil, which raises an error if dst exists and locking it if it doesn't:
def move2(src, dst):
 try:
 fd = os.open(dst, os.O_EXCL | os.O_CREAT)
 except OSError:
 raise Error('Destination exists')
 try:
 move(src, dst)
 finally:
 os.close(fd)
This could be optimised by using shutil.move code rather than just calling it, but the idea is that an attempt is made to create dst with exclusive access. If this fails, then it means that the file exists, but if it passes, then dst is locked so no other process can create it.
As suggested on the mailing list (http://mail.python.org/pipermail/python-ideas/2011-August/011132.html), an alternative is to add this behaviour as an argument to shutil.move, which may be a neater solution. 
I will work on a patch for this and try to submit it in the next few days.
msg141976 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011年08月12日 17:39
About new function vs. new argument: this could be asked on python-dev (or on the core-mentorship list if you prefer an environment guaranteed friendly). If you look at copy and copy2, you’ll see that adding a function was chosen, but the names are not helpful at all. In more recent shutil development, arguments like copy_function and ignore (on copytree) were added to let people customize behavior without rewriting all the function logic. Guido van Rossum has a rule about not adding an argument when the value is always a constant, preferring another function instead, but I’m not sure I remember it clearly, so asking for other developers’ thoughts would be good.
msg142052 - (view) Author: David Townshend (David.Townshend) Date: 2011年08月14日 06:01
A bit of research has shown that the proposed implementation will not work either, so my next suggestion is something along the lines of
def move2(src, dst):
 try:
 os.link(src, dst)
 except OSError as err:
 # handle error appropriately, raise shutil.Error if dst exists,
 # or use shutil.copy2 if dst is on a different filesystem.
 pass
 else:
 os.unlink(src)
History
Date User Action Args
2022年04月11日 14:57:20adminsetgithub: 56950
2017年02月08日 09:49:57steven.dapranosetnosy: + steven.daprano
2017年02月08日 09:49:35steven.dapranosetversions: + Python 3.7, - Python 3.3
2011年08月14日 06:01:02David.Townshendsetmessages: + msg142052
2011年08月12日 17:39:31eric.araujosetversions: + Python 3.3, - Python 3.2
type: behavior -> enhancement

nosy: + eric.araujo
title: Implementation of shutil.move -> Add function similar to shutil.move that does not overwrite
messages: + msg141976
stage: needs patch
2011年08月12日 12:41:05David.Townshendcreate

AltStyle によって変換されたページ (->オリジナル) /