annotate Lib/shutil.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 409448d455e3
children 602559fa5297
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11707
6f4146846337 The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents: 11202
diff changeset
1 """Utility functions for copying files and directory trees.
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
2
11202
7eeeb0d2b0a4 Typo in comment (on Mac, it's the *resource* fork that's not copied,
Guido van Rossum <guido@python.org>
parents: 10289
diff changeset
3 XXX The functions here don't copy the resource fork or other metadata on Mac.
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
4
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
5 """
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
771
2e82dd0f8a9e posix -> os
Guido van Rossum <guido@python.org>
parents: 363
diff changeset
7 import os
10289
1e766de714de Add import sys, needed by reference to sys.exc_info() in rmtree().
Guido van Rossum <guido@python.org>
parents: 9872
diff changeset
8 import sys
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
9 import stat
32386
686e5d38be42 shutil.move() will raise an exception when trying to move a directory into
Brett Cannon <bcannon@gmail.com>
parents: 28025
diff changeset
10 from os.path import abspath
47888
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
11 import fnmatch
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
16613
f68dbffff927 bunch more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 16084
diff changeset
13 __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2",
53652
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
14 "copytree","move","rmtree","Error", "SpecialFileError"]
25641
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
15
35505
26ba3d4f4fc4 No need to import exceptions, they are builtins
Neal Norwitz <nnorwitz@gmail.com>
parents: 35501
diff changeset
16 class Error(EnvironmentError):
25641
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
17 pass
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
53652
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
19 class SpecialFileError(EnvironmentError):
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
20 """Raised when trying to do a kind of operation (e.g. copying) which is
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
21 not supported on a special file (e.g. a named pipe)"""
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
22
48214
0aae72f8dda9 #3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
23 try:
0aae72f8dda9 #3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
24 WindowsError
0aae72f8dda9 #3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
25 except NameError:
0aae72f8dda9 #3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
26 WindowsError = None
0aae72f8dda9 #3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
27
13571
3a8c670309a2 apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents: 12285
diff changeset
28 def copyfileobj(fsrc, fdst, length=16*1024):
3a8c670309a2 apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents: 12285
diff changeset
29 """copy data from file-like object fsrc to file-like object fdst"""
3a8c670309a2 apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents: 12285
diff changeset
30 while 1:
3a8c670309a2 apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents: 12285
diff changeset
31 buf = fsrc.read(length)
3a8c670309a2 apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents: 12285
diff changeset
32 if not buf:
3a8c670309a2 apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents: 12285
diff changeset
33 break
3a8c670309a2 apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents: 12285
diff changeset
34 fdst.write(buf)
3a8c670309a2 apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents: 12285
diff changeset
35
33124
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32652
diff changeset
36 def _samefile(src, dst):
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32652
diff changeset
37 # Macintosh, Unix.
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32652
diff changeset
38 if hasattr(os.path,'samefile'):
33128
c2384601a241 Catch OSError raised when src or dst argument to os.path.samefile doesn't
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
39 try:
c2384601a241 Catch OSError raised when src or dst argument to os.path.samefile doesn't
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
40 return os.path.samefile(src, dst)
c2384601a241 Catch OSError raised when src or dst argument to os.path.samefile doesn't
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
41 except OSError:
c2384601a241 Catch OSError raised when src or dst argument to os.path.samefile doesn't
Johannes Gijsbers <jlg@dds.nl>
parents: 33124
diff changeset
42 return False
33124
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32652
diff changeset
43
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32652
diff changeset
44 # All other platforms: check for same pathname.
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32652
diff changeset
45 return (os.path.normcase(os.path.abspath(src)) ==
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32652
diff changeset
46 os.path.normcase(os.path.abspath(dst)))
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13571
diff changeset
47
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 def copyfile(src, dst):
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
49 """Copy data from src to dst"""
33124
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32652
diff changeset
50 if _samefile(src, dst):
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32652
diff changeset
51 raise Error, "`%s` and `%s` are the same file" % (src, dst)
dc647df53921 Raise an exception when src and dst refer to the same file via a hard link or a
Johannes Gijsbers <jlg@dds.nl>
parents: 32652
diff changeset
52
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
53 fsrc = None
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
54 fdst = None
53652
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
55 for fn in [src, dst]:
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
56 try:
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
57 st = os.stat(fn)
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
58 except OSError:
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
59 # File most likely does not exist
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
60 pass
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
61 # XXX What about other special files? (sockets, devices...)
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
62 if stat.S_ISFIFO(st.st_mode):
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
63 raise SpecialFileError("`%s` is a named pipe" % fn)
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
64 try:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
65 fsrc = open(src, 'rb')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
66 fdst = open(dst, 'wb')
13571
3a8c670309a2 apply patch #100868 from Moshe Zadka:
Greg Stein <gstein@lyra.org>
parents: 12285
diff changeset
67 copyfileobj(fsrc, fdst)
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
68 finally:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
69 if fdst:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
70 fdst.close()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
71 if fsrc:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
72 fsrc.close()
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 def copymode(src, dst):
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
75 """Copy mode bits from src to dst"""
16084
4c307a12dcb9 Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
76 if hasattr(os, 'chmod'):
4c307a12dcb9 Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
77 st = os.stat(src)
23697
c0e8c07ae1d6 Replace obsolete stat module constants with
Walter Dörwald <walter@livinglogic.de>
parents: 23627
diff changeset
78 mode = stat.S_IMODE(st.st_mode)
16084
4c307a12dcb9 Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
79 os.chmod(dst, mode)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 def copystat(src, dst):
40872
53fd144a3f1a Patch #1490190: posixmodule now includes os.chflags() and os.lchflags()
Martin v. Löwis <martin@v.loewis.de>
parents: 39307
diff changeset
82 """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
83 st = os.stat(src)
23697
c0e8c07ae1d6 Replace obsolete stat module constants with
Walter Dörwald <walter@livinglogic.de>
parents: 23627
diff changeset
84 mode = stat.S_IMODE(st.st_mode)
16084
4c307a12dcb9 Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
85 if hasattr(os, 'utime'):
23697
c0e8c07ae1d6 Replace obsolete stat module constants with
Walter Dörwald <walter@livinglogic.de>
parents: 23627
diff changeset
86 os.utime(dst, (st.st_atime, st.st_mtime))
16084
4c307a12dcb9 Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
87 if hasattr(os, 'chmod'):
4c307a12dcb9 Patch #103342: Make shutil.copytree more useful under Jython.
Tim Peters <tim.peters@gmail.com>
parents: 15872
diff changeset
88 os.chmod(dst, mode)
40872
53fd144a3f1a Patch #1490190: posixmodule now includes os.chflags() and os.lchflags()
Martin v. Löwis <martin@v.loewis.de>
parents: 39307
diff changeset
89 if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
53fd144a3f1a Patch #1490190: posixmodule now includes os.chflags() and os.lchflags()
Martin v. Löwis <martin@v.loewis.de>
parents: 39307
diff changeset
90 os.chflags(dst, st.st_flags)
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
91
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 def copy(src, dst):
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
94 """Copy data and mode bits ("cp src dst").
15872
3685ef2a55a4 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13571
diff changeset
95
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
96 The destination may be a directory.
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
97
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
98 """
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
99 if os.path.isdir(dst):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
100 dst = os.path.join(dst, os.path.basename(src))
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
101 copyfile(src, dst)
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
102 copymode(src, dst)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 def copy2(src, dst):
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
105 """Copy data and all stat info ("cp -p src dst").
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
106
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
107 The destination may be a directory.
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
108
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
109 """
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
110 if os.path.isdir(dst):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
111 dst = os.path.join(dst, os.path.basename(src))
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
112 copyfile(src, dst)
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
113 copystat(src, dst)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
47888
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
115 def ignore_patterns(*patterns):
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
116 """Function that can be used as copytree() ignore parameter.
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
117
47888
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
118 Patterns is a sequence of glob-style patterns
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
119 that are used to exclude files"""
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
120 def _ignore_patterns(path, names):
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
121 ignored_names = []
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
122 for pattern in patterns:
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
123 ignored_names.extend(fnmatch.filter(names, pattern))
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
124 return set(ignored_names)
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
125 return _ignore_patterns
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
126
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
127 def copytree(src, dst, symlinks=False, ignore=None):
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
128 """Recursively copy a directory tree using copy2().
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
129
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
130 The destination directory must not already exist.
28025
e63f1eccd250 Fix SF bug #691276, shutil.copytree documentation bug
Neal Norwitz <nnorwitz@gmail.com>
parents: 27361
diff changeset
131 If exception(s) occur, an Error is raised with a list of reasons.
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
132
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
133 If the optional symlinks flag is true, symbolic links in the
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
134 source tree result in symbolic links in the destination tree; if
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
135 it is false, the contents of the files pointed to by symbolic
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
136 links are copied.
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
137
47888
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
138 The optional ignore argument is a callable. If given, it
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
139 is called with the `src` parameter, which is the directory
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
140 being visited by copytree(), and `names` which is the list of
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
141 `src` contents, as returned by os.listdir():
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
142
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
143 callable(src, names) -> ignored_names
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
144
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
145 Since copytree() is called recursively, the callable will be
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
146 called once for each directory that is copied. It returns a
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
147 list of names relative to the `src` directory that should
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
148 not be copied.
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
149
5349
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
150 XXX Consider this example code rather than the ultimate tool.
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
151
6ab392d7a52f Transformed comments to doc strings.
Guido van Rossum <guido@python.org>
parents: 5348
diff changeset
152 """
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
153 names = os.listdir(src)
47888
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
154 if ignore is not None:
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
155 ignored_names = ignore(src, names)
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
156 else:
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
157 ignored_names = set()
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
158
34364
19e61a2a5632 Patch #1094015:
Johannes Gijsbers <jlg@dds.nl>
parents: 33865
diff changeset
159 os.makedirs(dst)
25641
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
160 errors = []
5348
ae02ed2aac3f Reindented at 4 spaces.
Guido van Rossum <guido@python.org>
parents: 5347
diff changeset
161 for name in names:
47888
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
162 if name in ignored_names:
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
163 continue
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
164 srcname = os.path.join(src, name)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
165 dstname = os.path.join(dst, name)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
166 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
167 if symlinks and os.path.islink(srcname):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
168 linkto = os.readlink(srcname)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
169 os.symlink(linkto, dstname)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
170 elif os.path.isdir(srcname):
47888
d7526670ff0a #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
Georg Brandl <georg@python.org>
parents: 45858
diff changeset
171 copytree(srcname, dstname, symlinks, ignore)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
172 else:
53652
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
173 # Will raise a SpecialFileError for unsupported file types
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 7203
diff changeset
174 copy2(srcname, dstname)
35501
ed65e0ad2215 patch [ 1242454 ] shutil.copytree() quits too soon after an error.
Georg Brandl <georg@python.org>
parents: 34485
diff changeset
175 # catch the Error from the recursive copytree so that we can
ed65e0ad2215 patch [ 1242454 ] shutil.copytree() quits too soon after an error.
Georg Brandl <georg@python.org>
parents: 34485
diff changeset
176 # continue with other files
ed65e0ad2215 patch [ 1242454 ] shutil.copytree() quits too soon after an error.
Georg Brandl <georg@python.org>
parents: 34485
diff changeset
177 except Error, err:
ed65e0ad2215 patch [ 1242454 ] shutil.copytree() quits too soon after an error.
Georg Brandl <georg@python.org>
parents: 34485
diff changeset
178 errors.extend(err.args[0])
53652
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
179 except EnvironmentError, why:
409448d455e3 Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
Antoine Pitrou <solipsis@pitrou.net>
parents: 51446
diff changeset
180 errors.append((srcname, dstname, str(why)))
39307
9377c6604542 Don't copy directory stat times in shutil.copytree on Windows
Martin v. Löwis <martin@v.loewis.de>
parents: 38032
diff changeset
181 try:
9377c6604542 Don't copy directory stat times in shutil.copytree on Windows
Martin v. Löwis <martin@v.loewis.de>
parents: 38032
diff changeset
182 copystat(src, dst)
9377c6604542 Don't copy directory stat times in shutil.copytree on Windows
Martin v. Löwis <martin@v.loewis.de>
parents: 38032
diff changeset
183 except OSError, why:
48214
0aae72f8dda9 #3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
184 if WindowsError is not None and isinstance(why, WindowsError):
0aae72f8dda9 #3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
185 # Copying file access times may fail on Windows
0aae72f8dda9 #3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
186 pass
0aae72f8dda9 #3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
187 else:
0aae72f8dda9 #3134: shutil referenced undefined WindowsError symbol
Antoine Pitrou <solipsis@pitrou.net>
parents: 47888
diff changeset
188 errors.extend((src, dst, str(why)))
25641
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
189 if errors:
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
190 raise Error, errors
7203
8c129a567867 Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents: 5349
diff changeset
191
27361
891ec4d266d1 rmtree(): Make implementation agree with documentation (both latex and
Barry Warsaw <barry@python.org>
parents: 27096
diff changeset
192 def rmtree(path, ignore_errors=False, onerror=None):
7203
8c129a567867 Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents: 5349
diff changeset
193 """Recursively delete a directory tree.
8c129a567867 Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents: 5349
diff changeset
194
33865
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
195 If ignore_errors is set, errors are ignored; otherwise, if onerror
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
196 is set, it is called to handle the error with arguments (func,
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
197 path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
198 path is the argument to that function that caused it to fail; and
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
199 exc_info is a tuple returned by sys.exc_info(). If ignore_errors
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
200 is false and onerror is None, an exception is raised.
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
201
7203
8c129a567867 Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents: 5349
diff changeset
202 """
33865
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
203 if ignore_errors:
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
204 def onerror(*args):
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
205 pass
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
206 elif onerror is None:
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
207 def onerror(*args):
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
208 raise
44823
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 40872
diff changeset
209 try:
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 40872
diff changeset
210 if os.path.islink(path):
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 40872
diff changeset
211 # symlinks to directories are forbidden, see bug #1669
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 40872
diff changeset
212 raise OSError("Cannot call rmtree on a symbolic link")
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 40872
diff changeset
213 except OSError:
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 40872
diff changeset
214 onerror(os.path.islink, path, sys.exc_info())
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 40872
diff changeset
215 # can't continue even if onerror hook returns
eaca2367ecc9 #1669: don't allow shutil.rmtree() to be called on a symlink.
Georg Brandl <georg@python.org>
parents: 40872
diff changeset
216 return
33865
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
217 names = []
27361
891ec4d266d1 rmtree(): Make implementation agree with documentation (both latex and
Barry Warsaw <barry@python.org>
parents: 27096
diff changeset
218 try:
33865
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
219 names = os.listdir(path)
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
220 except os.error, err:
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
221 onerror(os.listdir, path, sys.exc_info())
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
222 for name in names:
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
223 fullname = os.path.join(path, name)
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
224 try:
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
225 mode = os.lstat(fullname).st_mode
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
226 except os.error:
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
227 mode = 0
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
228 if stat.S_ISDIR(mode):
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
229 rmtree(fullname, ignore_errors, onerror)
27361
891ec4d266d1 rmtree(): Make implementation agree with documentation (both latex and
Barry Warsaw <barry@python.org>
parents: 27096
diff changeset
230 else:
33865
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
231 try:
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
232 os.remove(fullname)
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
233 except os.error, err:
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
234 onerror(os.remove, fullname, sys.exc_info())
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
235 try:
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
236 os.rmdir(path)
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
237 except os.error:
f196716ea0db Bug #1048941: shutil.rmtree error handling was always broken
Johannes Gijsbers <jlg@dds.nl>
parents: 33687
diff changeset
238 onerror(os.rmdir, path, sys.exc_info())
7203
8c129a567867 Added rmtree(), to recursively remove a directory tree.
Guido van Rossum <guido@python.org>
parents: 5349
diff changeset
239
45858
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
240
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
241 def _basename(path):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
242 # A basename() variant which first strips the trailing slash, if present.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
243 # Thus we always get the last component of the path, even for directories.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
244 return os.path.basename(path.rstrip(os.path.sep))
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
245
25641
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
246 def move(src, dst):
45858
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
247 """Recursively move a file or directory to another location. This is
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
248 similar to the Unix "mv" command.
25641
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
249
45858
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
250 If the destination is a directory or a symlink to a directory, the source
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
251 is moved inside the directory. The destination path must not already
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
252 exist.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
253
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
254 If the destination already exists but is not a directory, it may be
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
255 overwritten depending on os.rename() semantics.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
256
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
257 If the destination is on our current filesystem, then rename() is used.
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
258 Otherwise, src is copied to the destination and then removed.
25641
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
259 A lot more could be done here... A look at a mv.c shows a lot of
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
260 the issues this implementation glosses over.
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
261
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
262 """
45858
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
263 real_dst = dst
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
264 if os.path.isdir(dst):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
265 real_dst = os.path.join(dst, _basename(src))
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
266 if os.path.exists(real_dst):
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
267 raise Error, "Destination path '%s' already exists" % real_dst
25641
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
268 try:
45858
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
269 os.rename(src, real_dst)
25641
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
270 except OSError:
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
271 if os.path.isdir(src):
51446
0ab36796dd79 make destinsrc private
Benjamin Peterson <benjamin@python.org>
parents: 51179
diff changeset
272 if _destinsrc(src, dst):
32386
686e5d38be42 shutil.move() will raise an exception when trying to move a directory into
Brett Cannon <bcannon@gmail.com>
parents: 28025
diff changeset
273 raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
45858
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
274 copytree(src, real_dst, symlinks=True)
25641
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
275 rmtree(src)
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
276 else:
45858
923380acd1cd Issue 1577: shutil.move() where destination is a directory was doing a
Sean Reifscheider <jafo@tummy.com>
parents: 44823
diff changeset
277 copy2(src, real_dst)
25641
13438586daa6 Patch #448038: Add move(). Report errors from copytree as in shutil.Error.
Martin v. Löwis <martin@v.loewis.de>
parents: 25170
diff changeset
278 os.unlink(src)
32386
686e5d38be42 shutil.move() will raise an exception when trying to move a directory into
Brett Cannon <bcannon@gmail.com>
parents: 28025
diff changeset
279
51446
0ab36796dd79 make destinsrc private
Benjamin Peterson <benjamin@python.org>
parents: 51179
diff changeset
280 def _destinsrc(src, dst):
51179
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48214
diff changeset
281 src = abspath(src)
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48214
diff changeset
282 dst = abspath(dst)
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48214
diff changeset
283 if not src.endswith(os.path.sep):
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48214
diff changeset
284 src += os.path.sep
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48214
diff changeset
285 if not dst.endswith(os.path.sep):
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48214
diff changeset
286 dst += os.path.sep
e2dcad56e40e Merged revisions 69100 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 48214
diff changeset
287 return dst.startswith(src)