@@ -30,10 +30,8 @@ Some facts and figures:
3030 character devices and block devices and is able to acquire and restore file
3131 information like timestamp, access permissions and owner.
3232
33- * can handle tape devices.
3433
35-
36- .. function :: open(name[, mode[, fileobj[, bufsize]]], **kwargs)
34+ .. function :: open(name=None, mode='r', fileobj=None, bufsize=10240, \*\*kwargs)
3735
3836 Return a :class: `TarFile ` object for the pathname *name *. For detailed
3937 information on :class: `TarFile ` objects and the keyword arguments that are
@@ -74,7 +72,7 @@ Some facts and figures:
7472 for *name *. It is supposed to be at position 0.
7573
7674 For special purposes, there is a second format for *mode *:
77- ``'filemode|[compression]' ``. :func: `open ` will return a :class: `TarFile `
75+ ``'filemode|[compression]' ``. :func: `tarfile. open ` will return a :class: `TarFile `
7876 object that processes its data as a stream of blocks. No random seeking will
7977 be done on the file. If given, *fileobj * may be any object that has a
8078 :meth: `read ` or :meth: `write ` method (depending on the *mode *). *bufsize *
@@ -112,7 +110,7 @@ Some facts and figures:
112110.. class :: TarFile
113111
114112 Class for reading and writing tar archives. Do not use this class directly,
115- better use :func: `open ` instead. See :ref: `tarfile-objects `.
113+ better use :func: `tarfile. open ` instead. See :ref: `tarfile-objects `.
116114
117115
118116.. function :: is_tarfile(name)
@@ -121,7 +119,7 @@ Some facts and figures:
121119 module can read.
122120
123121
124- .. class :: TarFileCompat(filename[ , mode[ , compression]] )
122+ .. class :: TarFileCompat(filename, mode='r' , compression=TAR_PLAIN )
125123
126124 Class for limited access to tar archives with a :mod: `zipfile `\ -like interface.
127125 Please consult the documentation of the :mod: `zipfile ` module for more details.
@@ -163,13 +161,14 @@ Some facts and figures:
163161
164162.. exception :: ExtractError
165163
166- Is raised for *non-fatal * errors when using :meth: `extract `, but only if
164+ Is raised for *non-fatal * errors when using :meth: `TarFile. extract `, but only if
167165 :attr: `TarFile.errorlevel `\ ``== 2 ``.
168166
169167
170168.. exception :: HeaderError
171169
172- Is raised by :meth: `frombuf ` if the buffer it gets is invalid.
170+ Is raised by :meth: `TarInfo.frombuf ` if the buffer it gets is invalid.
171+
173172
174173
175174Each of the following constants defines a tar archive format that the
@@ -197,12 +196,21 @@ details.
197196 The default format for creating archives. This is currently :const: `GNU_FORMAT `.
198197
199198
199+ The following variables are available on module level:
200+
201+
202+ .. data :: ENCODING
203+
204+ The default character encoding i.e. the value from either
205+ :func: `sys.getfilesystemencoding ` or :func: `sys.getdefaultencoding `.
206+
207+
200208.. seealso ::
201209
202210 Module :mod: `zipfile `
203211 Documentation of the :mod: `zipfile ` standard module.
204212
205- `GNU tar manual, Basic Tar Format <http://www.gnu.org/software/tar/manual/html_node/tar_134 .html#SEC134 >`_
213+ `GNU tar manual, Basic Tar Format <http://www.gnu.org/software/tar/manual/html_node/Standard .html >`_
206214 Documentation for tar archive files, including GNU tar extensions.
207215
208216
@@ -218,7 +226,7 @@ archive several times. Each archive member is represented by a :class:`TarInfo`
218226object, see :ref: `tarinfo-objects ` for details.
219227
220228
221- .. class :: TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=None , errors=None, pax_headers=None, debug=0, errorlevel=0)
229+ .. class :: TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=ENCODING , errors=None, pax_headers=None, debug=0, errorlevel=0)
222230
223231 All following arguments are optional and can be accessed as instance attributes
224232 as well.
@@ -245,18 +253,18 @@ object, see :ref:`tarinfo-objects` for details.
245253 The *tarinfo * argument can be used to replace the default :class: `TarInfo ` class
246254 with a different one.
247255
248- If *dereference * is `` False ` `, add symbolic and hard links to the archive. If it
249- is `` True ` `, add the content of the target files to the archive. This has no
256+ If *dereference * is :const: ` False `, add symbolic and hard links to the archive. If it
257+ is :const: ` True `, add the content of the target files to the archive. This has no
250258 effect on systems that do not support symbolic links.
251259
252- If *ignore_zeros * is `` False ` `, treat an empty block as the end of the archive.
253- If it is * True * , skip empty (and invalid) blocks and try to get as many members
260+ If *ignore_zeros * is :const: ` False `, treat an empty block as the end of the archive.
261+ If it is :const: ` True ` , skip empty (and invalid) blocks and try to get as many members
254262 as possible. This is only useful for reading concatenated or damaged archives.
255263
256264 *debug * can be set from ``0 `` (no debug messages) up to ``3 `` (all debug
257265 messages). The messages are written to ``sys.stderr ``.
258266
259- If *errorlevel * is ``0 ``, all errors are ignored when using :meth: `extract `.
267+ If *errorlevel * is ``0 ``, all errors are ignored when using :meth: `TarFile. extract `.
260268 Nevertheless, they appear as error messages in the debug output, when debugging
261269 is enabled. If ``1 ``, all *fatal * errors are raised as :exc: `OSError ` or
262270 :exc: `IOError ` exceptions. If ``2 ``, all *non-fatal * errors are raised as
@@ -273,8 +281,8 @@ object, see :ref:`tarinfo-objects` for details.
273281
274282.. method :: TarFile.open(...)
275283
276- Alternative constructor. The :func: `open ` function on module level is actually a
277- shortcut to this classmethod. See section :ref: ` tarfile-mod ` for details.
284+ Alternative constructor. The :func: `tarfile. open ` function is actually a
285+ shortcut to this classmethod.
278286
279287
280288.. method :: TarFile.getmember(name)
@@ -310,11 +318,11 @@ object, see :ref:`tarinfo-objects` for details.
310318.. method :: TarFile.next()
311319
312320 Return the next member of the archive as a :class: `TarInfo ` object, when
313- :class: `TarFile ` is opened for reading. Return `` None ` ` if there is no more
321+ :class: `TarFile ` is opened for reading. Return :const: ` None ` if there is no more
314322 available.
315323
316324
317- .. method :: TarFile.extractall([ path[ , members]] )
325+ .. method :: TarFile.extractall(path="." , members=None )
318326
319327 Extract all members from the archive to the current working directory or
320328 directory *path *. If optional *members * is given, it must be a subset of the
@@ -332,7 +340,7 @@ object, see :ref:`tarinfo-objects` for details.
332340 dots ``".." ``.
333341
334342
335- .. method :: TarFile.extract(member[ , path] )
343+ .. method :: TarFile.extract(member, path="" )
336344
337345 Extract a member from the archive to the current working directory, using its
338346 full name. Its file information is extracted as accurately as possible. *member *
@@ -341,9 +349,8 @@ object, see :ref:`tarinfo-objects` for details.
341349
342350 .. note ::
343351
344- Because the :meth: `extract ` method allows random access to a tar archive there
345- are some issues you must take care of yourself. See the description for
346- :meth: `extractall ` above.
352+ The :meth: `extract ` method does not take care of several extraction issues.
353+ In most cases you should consider using the :meth: `extractall ` method.
347354
348355 .. warning ::
349356
@@ -355,15 +362,15 @@ object, see :ref:`tarinfo-objects` for details.
355362 Extract a member from the archive as a file object. *member * may be a filename
356363 or a :class: `TarInfo ` object. If *member * is a regular file, a file-like object
357364 is returned. If *member * is a link, a file-like object is constructed from the
358- link's target. If *member * is none of the above, `` None ` ` is returned.
365+ link's target. If *member * is none of the above, :const: ` None ` is returned.
359366
360367 .. note ::
361368
362369 The file-like object is read-only and provides the following methods:
363370 :meth: `read `, :meth: `readline `, :meth: `readlines `, :meth: `seek `, :meth: `tell `.
364371
365372
366- .. method :: TarFile.add(name[ , arcname[ , recursive[ , exclude]]] )
373+ .. method :: TarFile.add(name, arcname=None , recursive=True , exclude=None )
367374
368375 Add the file *name * to the archive. *name * may be any type of file (directory,
369376 fifo, symbolic link, etc.). If given, *arcname * specifies an alternative name
@@ -374,7 +381,7 @@ object, see :ref:`tarinfo-objects` for details.
374381 (:const: `True `) or added (:const: `False `).
375382
376383
377- .. method :: TarFile.addfile(tarinfo[ , fileobj] )
384+ .. method :: TarFile.addfile(tarinfo, fileobj=None )
378385
379386 Add the :class: `TarInfo ` object *tarinfo * to the archive. If *fileobj * is given,
380387 ``tarinfo.size `` bytes are read from it and added to the archive. You can
@@ -386,7 +393,7 @@ object, see :ref:`tarinfo-objects` for details.
386393 avoid irritation about the file size.
387394
388395
389- .. method :: TarFile.gettarinfo([ name[ , arcname[ , fileobj]]] )
396+ .. method :: TarFile.gettarinfo(name=None , arcname=None , fileobj=None )
390397
391398 Create a :class: `TarInfo ` object for either the file *name * or the file object
392399 *fileobj * (using :func: `os.fstat ` on its file descriptor). You can modify some
@@ -432,7 +439,7 @@ It does *not* contain the file's data itself.
432439:meth: `getmember `, :meth: `getmembers ` and :meth: `gettarinfo `.
433440
434441
435- .. class :: TarInfo([ name] )
442+ .. class :: TarInfo(name="" )
436443
437444 Create a :class: `TarInfo ` object.
438445
@@ -450,7 +457,7 @@ It does *not* contain the file's data itself.
450457 a :class: `TarInfo ` object.
451458
452459
453- .. method :: TarInfo.tobuf([ format[ , encoding [ , errors]]] )
460+ .. method :: TarInfo.tobuf(format=DEFAULT_FORMAT , encoding=ENCODING , errors='strict' )
454461
455462 Create a string buffer from a :class: `TarInfo ` object. For information on the
456463 arguments see the constructor of the :class: `TarFile ` class.
@@ -579,6 +586,21 @@ How to extract an entire tar archive to the current working directory::
579586 tar.extractall()
580587 tar.close()
581588
589+ How to extract a subset of a tar archive with :meth: `TarFile.extractall ` using
590+ a generator function instead of a list::
591+
592+ import os
593+ import tarfile
594+
595+ def py_files(members):
596+ for tarinfo in members:
597+ if os.path.splitext(tarinfo.name)[1] == ".py":
598+ yield tarinfo
599+
600+ tar = tarfile.open("sample.tar.gz")
601+ tar.extractall(members=py_files(tar))
602+ tar.close()
603+
582604How to create an uncompressed tar archive from a list of filenames::
583605
584606 import tarfile
@@ -601,28 +623,6 @@ How to read a gzip compressed tar archive and display some member information::
601623 print("something else.")
602624 tar.close()
603625
604- How to create a tar archive with faked information::
605-
606- import tarfile
607- tar = tarfile.open("sample.tar.gz", "w:gz")
608- for name in namelist:
609- tarinfo = tar.gettarinfo(name, "fakeproj-1.0/" + name)
610- tarinfo.uid = 123
611- tarinfo.gid = 456
612- tarinfo.uname = "johndoe"
613- tarinfo.gname = "fake"
614- tar.addfile(tarinfo, file(name))
615- tar.close()
616-
617- The *only * way to extract an uncompressed tar stream from ``sys.stdin ``::
618-
619- import sys
620- import tarfile
621- tar = tarfile.open(mode="r|", fileobj=sys.stdin)
622- for tarinfo in tar:
623- tar.extract(tarinfo)
624- tar.close()
625-
626626
627627.. _tar-formats :
628628
0 commit comments