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

Skip to content

Commit 0929b1f

Browse files
committed
Add entry for shutil's archiving operations.
1 parent f2e439f commit 0929b1f

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

Doc/library/shutil.rst

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,19 +234,21 @@ Another example that uses the *ignore* argument to add a logging call::
234234
copytree(source, destination, ignore=_logpath)
235235

236236

237-
Archives operations
238-
-------------------
237+
.. _archiving-operations:
238+
239+
Archiving operations
240+
--------------------
239241

240242
.. function:: make_archive(base_name, format, [root_dir, [base_dir, [verbose, [dry_run, [owner, [group, [logger]]]]]]])
241243

242-
Create an archive file (e.g. zip or tar) and returns its name.
244+
Create an archive file (such as zip or tar) and return its name.
243245

244246
*base_name* is the name of the file to create, including the path, minus
245247
any format-specific extension. *format* is the archive format: one of
246248
"zip", "tar", "bztar" (if the :mod:`bz2` module is available) or "gztar".
247249

248250
*root_dir* is a directory that will be the root directory of the
249-
archive; i.e. we typically chdir into *root_dir* before creating the
251+
archive; for example, we typically chdir into *root_dir* before creating the
250252
archive.
251253

252254
*base_dir* is the directory where we start archiving from;
@@ -258,6 +260,8 @@ Archives operations
258260
*owner* and *group* are used when creating a tar archive. By default,
259261
uses the current owner and group.
260262

263+
*logger* is an instance of :class:`logging.Logger`.
264+
261265
.. versionadded:: 3.2
262266

263267

@@ -284,7 +288,7 @@ Archives operations
284288
Registers an archiver for the format *name*. *function* is a callable that
285289
will be used to invoke the archiver.
286290

287-
If given, *extra_args* is a sequence of ``(name, value)`` that will be
291+
If given, *extra_args* is a sequence of ``(name, value)`` pairs that will be
288292
used as extra keywords arguments when the archiver callable is used.
289293

290294
*description* is used by :func:`get_archive_formats` which returns the
@@ -316,7 +320,7 @@ Archives operations
316320
.. versionadded:: 3.2
317321

318322

319-
.. function:: register_unpack_format(name, extensions, function[, extra_args[,description]])
323+
.. function:: register_unpack_format(name, extensions, function[, extra_args[, description]])
320324

321325
Registers an unpack format. *name* is the name of the format and
322326
*extensions* is a list of extensions corresponding to the format, like

Doc/whatsnew/3.2.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,40 @@ The :func:`shutil.copytree` function has two new options:
11941194

11951195
(Contributed by Tarek Ziadé.)
11961196

1197+
In addition, the :mod:`shutil` module now supports :ref:`archiving operations
1198+
<archiving-operations>` for zipfiles, uncompressed tarfiles, gzipped tarfiles,
1199+
and bzipped tarfiles. And there are functions for registering additional
1200+
archiving file formats (such as xz compressed tarfiles or custom formats).
1201+
1202+
The principal functions are :func:`~shutil.make_archive` and
1203+
:func:`~shutil.unpack_archive`. By default, both operate on the current
1204+
directory (which can be set by :func:`os.chdir`) and on any sub-directories.
1205+
The archive filename needs to specified with a full pathname. The archiving
1206+
step is non-destructive (the original files are left unchanged).
1207+
1208+
::
1209+
1210+
>>> import shutil, pprint
1211+
>>> os.chdir('mydata') # change to the source directory
1212+
>>> f = make_archive('/var/backup/mydata', 'zip') # archive the current directory
1213+
>>> f # show the name of archive
1214+
'/var/backup/mydata.zip'
1215+
>>> os.chdir('tmp') # change to an unpacking
1216+
>>> shutil.unpack_archive('/var/backup/mydata.zip') # recover the data
1217+
>>> pprint.pprint(shutil.get_archive_formats()) # display known formats
1218+
[('bztar', "bzip2'ed tar-file"),
1219+
('gztar', "gzip'ed tar-file"),
1220+
('tar', 'uncompressed tar file'),
1221+
('zip', 'ZIP file')]
1222+
>>> shutil.register_archive_format( # register a new archive format
1223+
name = 'xz',
1224+
function = 'xz.compress',
1225+
extra_args = [('level', 8)],
1226+
description = 'xz compression'
1227+
)
1228+
1229+
(Contributed by Tarek Ziadé.)
1230+
11971231
sqlite3
11981232
-------
11991233

@@ -1663,6 +1697,8 @@ reading directly from dictionaries and strings.
16631697
- non-UTF8 percent encoding of non-ASCII characters
16641698
Issue 2987 for IPv6 (RFC2732) support in urlparse
16651699
1700+
.. XXX reprlib.recursive_repr
1701+
16661702
16671703
Multi-threading
16681704
===============

0 commit comments

Comments
 (0)