22
22
can't copy all file metadata.
24
24
On POSIX platforms, this means that file owner and group are lost as well
25
as ACLs. On MacOS, the resource fork and other metadata are not used.
25
as ACLs. On Mac OS, the resource fork and other metadata are not used.
26
26
This means that resources will be lost and file type and creator codes will
27
27
not be correct. On Windows, file owners, ACLs and alternate data streams
78
78
Unix command :program:`cp -p`.
81
.. function:: copytree(src, dst[, symlinks])
81
.. function:: ignore_patterns(\*patterns)
83
This factory function creates a function that can be used as a callable for
84
:func:`copytree`\'s *ignore* argument, ignoring files and directories that
85
match one of the glob-style *patterns* provided. See the example below.
90
.. function:: copytree(src, dst[, symlinks=False[, ignore=None]])
83
92
Recursively copy an entire directory tree rooted at *src*. The destination
84
directory, named by *dst*, must not already exist; it will be created as well as
85
missing parent directories. Permissions and times of directories are copied with
86
:func:`copystat`, individual files are copied using :func:`copy2`. If
87
*symlinks* is true, symbolic links in the source tree are represented as
88
symbolic links in the new tree; if false or omitted, the contents of the linked
89
files are copied to the new tree. If exception(s) occur, an :exc:`Error` is
90
raised with a list of reasons.
92
The source code for this should be considered an example rather than a tool.
93
directory, named by *dst*, must not already exist; it will be created as well
94
as missing parent directories. Permissions and times of directories are
95
copied with :func:`copystat`, individual files are copied using
98
If *symlinks* is true, symbolic links in the source tree are represented as
99
symbolic links in the new tree; if false or omitted, the contents of the
100
linked files are copied to the new tree.
102
If *ignore* is given, it must be a callable that will receive as its
103
arguments the directory being visited by :func:`copytree`, and a list of its
104
contents, as returned by :func:`os.listdir`. Since :func:`copytree` is
105
called recursively, the *ignore* callable will be called once for each
106
directory that is copied. The callable must return a sequence of directory
107
and file names relative to the current directory (i.e. a subset of the items
108
in its second argument); these names will then be ignored in the copy
109
process. :func:`ignore_patterns` can be used to create such a callable that
110
ignores names based on glob-style patterns.
112
If exception(s) occur, an :exc:`Error` is raised with a list of reasons.
114
The source code for this should be considered an example rather than the
94
117
.. versionchanged:: 2.3
95
118
:exc:`Error` is raised if any exceptions occur during copying, rather than
152
178
above, with the docstring omitted. It demonstrates many of the other functions
153
179
provided by this module. ::
155
def copytree(src, dst, symlinks=False):
181
def copytree(src, dst, symlinks=False, ignore=None):
156
182
names = os.listdir(src)
183
if ignore is not None:
184
ignored_names = ignore(src, names)
186
ignored_names = set()
159
190
for name in names:
191
if name in ignored_names:
160
193
srcname = os.path.join(src, name)
161
194
dstname = os.path.join(dst, name)
164
197
linkto = os.readlink(srcname)
165
198
os.symlink(linkto, dstname)
166
199
elif os.path.isdir(srcname):
167
copytree(srcname, dstname, symlinks)
200
copytree(srcname, dstname, symlinks, ignore)
169
202
copy2(srcname, dstname)
170
203
# XXX What about devices, sockets etc.?
183
216
errors.extend((src, dst, str(why)))
185
218
raise Error, errors
220
Another example that uses the :func:`ignore_patterns` helper::
222
from shutil import copytree, ignore_patterns
224
copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
226
This will copy everything except ``.pyc`` files and files or directories whose
227
name starts with ``tmp``.
229
Another example that uses the *ignore* argument to add a logging call::
231
from shutil import copytree
234
def _logpath(path, names):
235
logging.info('Working in %s' % path)
236
return [] # nothing will be ignored
238
copytree(source, destination, ignore=_logpath)