@@ -26,6 +26,8 @@ copying and removal. For operations on individual files, see also the
2626 not be correct. On Windows, file owners, ACLs and alternate data streams
2727 are not copied.
2828
29+ Directory and files operations
30+ ------------------------------
2931
3032.. function :: copyfileobj(fsrc, fdst[, length])
3133
@@ -150,8 +152,8 @@ copying and removal. For operations on individual files, see also the
150152
151153.. _shutil-example :
152154
153- Example
154- -------
155+ copytree example
156+ ::::::::::::::::
155157
156158This example is the implementation of the :func: `copytree ` function, described
157159above, with the docstring omitted. It demonstrates many of the other functions
@@ -189,3 +191,117 @@ provided by this module. ::
189191 if errors:
190192 raise Error(errors)
191193
194+ Another example that uses the :func: `ignore_patterns ` helper::
195+
196+ from shutil import copytree, ignore_patterns
197+
198+ copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
199+
200+ This will copy everything except ``.pyc `` files and files or directories whose
201+ name starts with ``tmp ``.
202+
203+ Another example that uses the *ignore * argument to add a logging call::
204+
205+ from shutil import copytree
206+ import logging
207+
208+ def _logpath(path, names):
209+ logging.info('Working in %s' % path)
210+ return [] # nothing will be ignored
211+
212+ copytree(source, destination, ignore=_logpath)
213+
214+
215+ Archives operations
216+ -------------------
217+
218+ .. function :: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]])
219+
220+ Create an archive file (eg. zip or tar) and returns its name.
221+
222+ *base_name * is the name of the file to create, including the path, minus
223+ any format-specific extension. *format * is the archive format: one of
224+ "zip", "tar", "ztar", or "gztar".
225+
226+ *root_dir * is a directory that will be the root directory of the
227+ archive; ie. we typically chdir into *root_dir * before creating the
228+ archive.
229+
230+ *base_dir * is the directory where we start archiving from;
231+ ie. *base_dir * will be the common prefix of all files and
232+ directories in the archive.
233+
234+ *root_dir * and *base_dir * both default to the current directory.
235+
236+ *owner * and *group * are used when creating a tar archive. By default,
237+ uses the current owner and group.
238+
239+ .. versionadded :: 2.7
240+
241+
242+ .. function :: get_archive_formats()
243+
244+ Returns a list of supported formats for archiving.
245+ Each element of the returned sequence is a tuple ``(name, description) ``
246+
247+ By default :mod: `shutil ` provides these formats:
248+
249+ - *gztar *: gzip'ed tar-file
250+ - *bztar *: bzip2'ed tar-file
251+ - *ztar *: compressed tar file
252+ - *tar *: uncompressed tar file
253+ - *zip *: ZIP file
254+
255+ You can register new formats or provide your own archiver for any existing
256+ formats, by using :func: `register_archive_format `.
257+
258+ .. versionadded :: 2.7
259+
260+
261+ .. function :: register_archive_format(name, function, [extra_args, [description]])
262+
263+ Registers an archiver for the format *name *. *function * is a callable that
264+ will be used to invoke the archiver.
265+
266+ If given, *extra_args * is a sequence of ``(name, value) `` that will be
267+ used as extra keywords arguments when the archiver callable is used.
268+
269+ *description * is used by :func: `get_archive_formats ` which returns the
270+ list of archivers. Defaults to an empty list.
271+
272+ .. versionadded :: 2.7
273+
274+
275+ .. function :: unregister_archive_format(name)
276+
277+ Remove the archive format *name * from the list of supported formats.
278+
279+ .. versionadded :: 2.7
280+
281+
282+ Archiving example
283+ :::::::::::::::::
284+
285+ In this example, we create a gzip'ed tar-file archive containing all files
286+ found in the :file: `.ssh ` directory of the user::
287+
288+ >>> from shutil import make_archive
289+ >>> import os
290+ >>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
291+ >>> root_dir = os.path.expanduser(os.path.join('~', '.ssh'))
292+ >>> make_archive(archive_name, 'gztar', root_dir)
293+ '/Users/tarek/myarchive.tar.gz'
294+
295+ The resulting archive contains::
296+
297+ $ tar -tzvf /Users/tarek/myarchive.tar.gz
298+ drwx------ tarek/staff 0 2010-02-01 16:23:40 ./
299+ -rw-r--r-- tarek/staff 609 2008-06-09 13:26:54 ./authorized_keys
300+ -rwxr-xr-x tarek/staff 65 2008-06-09 13:26:54 ./config
301+ -rwx------ tarek/staff 668 2008-06-09 13:26:54 ./id_dsa
302+ -rwxr-xr-x tarek/staff 609 2008-06-09 13:26:54 ./id_dsa.pub
303+ -rw------- tarek/staff 1675 2008-06-09 13:26:54 ./id_rsa
304+ -rw-r--r-- tarek/staff 397 2008-06-09 13:26:54 ./id_rsa.pub
305+ -rw-r--r-- tarek/staff 37192 2010-02-06 18:23:10 ./known_hosts
306+
307+
0 commit comments