Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e9ce0b0

Browse files
committed
Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
1 parent 114619e commit e9ce0b0

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

Doc/lib/libshutil.tex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ \section{\module{shutil} ---
9393
not be caught.
9494
\end{funcdesc}
9595

96+
\begin{funcdesc}{move}{src, dst}
97+
Recursively move a file or directory to another location.
98+
99+
If the destination is on our current filesystem, then simply use
100+
rename. Otherwise, copy src to the dst and then remove src.
101+
102+
\versionadded{2.3}
103+
\end{funcdesc}
104+
105+
\begin{excdesc}{Error}
106+
This exception collects exceptions that raised during a mult-file
107+
operation. For \function{copytree}, the exception argument is a
108+
list of 3-tuples (\var{srcname}, \var{dstname}, \var{exception}).
109+
110+
\versionadded{2.3}
111+
\end{excdesc}
96112

97113
\subsection{Example \label{shutil-example}}
98114

Lib/shutil.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
import os
88
import sys
99
import stat
10+
import exceptions
1011

1112
__all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2",
12-
"copytree","rmtree"]
13+
"copytree","rmtree","Error"]
14+
15+
class Error(exceptions.EnvironmentError):
16+
pass
1317

1418
def copyfileobj(fsrc, fdst, length=16*1024):
1519
"""copy data from file-like object fsrc to file-like object fdst"""
@@ -95,6 +99,7 @@ def copytree(src, dst, symlinks=0):
9599
"""
96100
names = os.listdir(src)
97101
os.mkdir(dst)
102+
errors = []
98103
for name in names:
99104
srcname = os.path.join(src, name)
100105
dstname = os.path.join(dst, name)
@@ -108,7 +113,9 @@ def copytree(src, dst, symlinks=0):
108113
copy2(srcname, dstname)
109114
# XXX What about devices, sockets etc.?
110115
except (IOError, os.error), why:
111-
print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))
116+
errors.append((srcname, dstname, why))
117+
if errors:
118+
raise Error, errors
112119

113120
def rmtree(path, ignore_errors=0, onerror=None):
114121
"""Recursively delete a directory tree.
@@ -141,3 +148,24 @@ def _build_cmdtuple(path, cmdtuples):
141148
else:
142149
cmdtuples.append((os.remove, real_f))
143150
cmdtuples.append((os.rmdir, path))
151+
152+
153+
def move(src, dst):
154+
"""Recursively move a file or directory to another location.
155+
156+
If the destination is on our current filesystem, then simply use
157+
rename. Otherwise, copy src to the dst and then remove src.
158+
A lot more could be done here... A look at a mv.c shows a lot of
159+
the issues this implementation glosses over.
160+
161+
"""
162+
163+
try:
164+
os.rename(src, dst)
165+
except OSError:
166+
if os.path.isdir(src):
167+
copytree(src, dst, symlinks=1)
168+
rmtree(src)
169+
else:
170+
copy2(src,dst)
171+
os.unlink(src)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ Extension modules
342342
Library
343343
-------
344344

345+
- shutil.move was added. shutil.copytree now reports errors as an
346+
exception at the end, instead of printing error messages.
347+
345348
- Encoding name normalization was generalized to not only
346349
replace hyphens with underscores, but also all other non-alphanumeric
347350
characters (with the exception of the dot which is used for Python

0 commit comments

Comments
 (0)