blob: f7e339f7fdb7525f6b992291fcdc6fff41e76fbb [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. _tarfile-mod:
2
3:mod:`tarfile` --- Read and write tar archive files
4===================================================
5
6.. module:: tarfile
7 :synopsis: Read and write tar-format archive files.
8
9
10.. versionadded:: 2.3
11
12.. moduleauthor:: Lars Gustäbel <lars@gustaebel.de>
13.. sectionauthor:: Lars Gustäbel <lars@gustaebel.de>
14
15
16The :mod:`tarfile` module makes it possible to read and create tar archives.
17Some facts and figures:
18
19* reads and writes :mod:`gzip` and :mod:`bzip2` compressed archives.
20
21* read/write support for the POSIX.1-1988 (ustar) format.
22
23* read/write support for the GNU tar format including *longname* and *longlink*
24 extensions, read-only support for the *sparse* extension.
25
26* read/write support for the POSIX.1-2001 (pax) format.
27
28 .. versionadded:: 2.6
29
30* handles directories, regular files, hardlinks, symbolic links, fifos,
31 character devices and block devices and is able to acquire and restore file
32 information like timestamp, access permissions and owner.
33
34* can handle tape devices.
35
36
37.. function:: open(name[, mode[, fileobj[, bufsize]]], **kwargs)
38
39 Return a :class:`TarFile` object for the pathname *name*. For detailed
40 information on :class:`TarFile` objects and the keyword arguments that are
41 allowed, see :ref:`tarfile-objects`.
42
43 *mode* has to be a string of the form ``'filemode[:compression]'``, it defaults
44 to ``'r'``. Here is a full list of mode combinations:
45
46 +------------------+---------------------------------------------+
47 | mode | action |
48 +==================+=============================================+
49 | ``'r' or 'r:*'`` | Open for reading with transparent |
50 | | compression (recommended). |
51 +------------------+---------------------------------------------+
52 | ``'r:'`` | Open for reading exclusively without |
53 | | compression. |
54 +------------------+---------------------------------------------+
55 | ``'r:gz'`` | Open for reading with gzip compression. |
56 +------------------+---------------------------------------------+
57 | ``'r:bz2'`` | Open for reading with bzip2 compression. |
58 +------------------+---------------------------------------------+
59 | ``'a' or 'a:'`` | Open for appending with no compression. The |
60 | | file is created if it does not exist. |
61 +------------------+---------------------------------------------+
62 | ``'w' or 'w:'`` | Open for uncompressed writing. |
63 +------------------+---------------------------------------------+
64 | ``'w:gz'`` | Open for gzip compressed writing. |
65 +------------------+---------------------------------------------+
66 | ``'w:bz2'`` | Open for bzip2 compressed writing. |
67 +------------------+---------------------------------------------+
68
69 Note that ``'a:gz'`` or ``'a:bz2'`` is not possible. If *mode* is not suitable
70 to open a certain (compressed) file for reading, :exc:`ReadError` is raised. Use
71 *mode* ``'r'`` to avoid this. If a compression method is not supported,
72 :exc:`CompressionError` is raised.
73
74 If *fileobj* is specified, it is used as an alternative to a file object opened
75 for *name*. It is supposed to be at position 0.
76
77 For special purposes, there is a second format for *mode*:
78 ``'filemode|[compression]'``. :func:`open` will return a :class:`TarFile`
79 object that processes its data as a stream of blocks. No random seeking will
80 be done on the file. If given, *fileobj* may be any object that has a
81 :meth:`read` or :meth:`write` method (depending on the *mode*). *bufsize*
82 specifies the blocksize and defaults to ``20 * 512`` bytes. Use this variant
83 in combination with e.g. ``sys.stdin``, a socket file object or a tape
84 device. However, such a :class:`TarFile` object is limited in that it does
85 not allow to be accessed randomly, see :ref:`tar-examples`. The currently
86 possible modes:
87
88 +-------------+--------------------------------------------+
89 | Mode | Action |
90 +=============+============================================+
91 | ``'r|*'`` | Open a *stream* of tar blocks for reading |
92 | | with transparent compression. |
93 +-------------+--------------------------------------------+
94 | ``'r|'`` | Open a *stream* of uncompressed tar blocks |
95 | | for reading. |
96 +-------------+--------------------------------------------+
97 | ``'r|gz'`` | Open a gzip compressed *stream* for |
98 | | reading. |
99 +-------------+--------------------------------------------+
100 | ``'r|bz2'`` | Open a bzip2 compressed *stream* for |
101 | | reading. |
102 +-------------+--------------------------------------------+
103 | ``'w|'`` | Open an uncompressed *stream* for writing. |
104 +-------------+--------------------------------------------+
105 | ``'w|gz'`` | Open an gzip compressed *stream* for |
106 | | writing. |
107 +-------------+--------------------------------------------+
108 | ``'w|bz2'`` | Open an bzip2 compressed *stream* for |
109 | | writing. |
110 +-------------+--------------------------------------------+
111
112
113.. class:: TarFile
114
115 Class for reading and writing tar archives. Do not use this class directly,
116 better use :func:`open` instead. See :ref:`tarfile-objects`.
117
118
119.. function:: is_tarfile(name)
120
121 Return :const:`True` if *name* is a tar archive file, that the :mod:`tarfile`
122 module can read.
123
124
125.. class:: TarFileCompat(filename[, mode[, compression]])
126
127 Class for limited access to tar archives with a :mod:`zipfile`\ -like interface.
128 Please consult the documentation of the :mod:`zipfile` module for more details.
129 *compression* must be one of the following constants:
130
131
132 .. data:: TAR_PLAIN
133
134 Constant for an uncompressed tar archive.
135
136
137 .. data:: TAR_GZIPPED
138
139 Constant for a :mod:`gzip` compressed tar archive.
140
141
142.. exception:: TarError
143
144 Base class for all :mod:`tarfile` exceptions.
145
146
147.. exception:: ReadError
148
149 Is raised when a tar archive is opened, that either cannot be handled by the
150 :mod:`tarfile` module or is somehow invalid.
151
152
153.. exception:: CompressionError
154
155 Is raised when a compression method is not supported or when the data cannot be
156 decoded properly.
157
158
159.. exception:: StreamError
160
161 Is raised for the limitations that are typical for stream-like :class:`TarFile`
162 objects.
163
164
165.. exception:: ExtractError
166
167 Is raised for *non-fatal* errors when using :meth:`extract`, but only if
168 :attr:`TarFile.errorlevel`\ ``== 2``.
169
170
171.. exception:: HeaderError
172
173 Is raised by :meth:`frombuf` if the buffer it gets is invalid.
174
175 .. versionadded:: 2.6
176
177Each of the following constants defines a tar archive format that the
178:mod:`tarfile` module is able to create. See section :ref:`tar-formats` for
179details.
180
181
182.. data:: USTAR_FORMAT
183
184 POSIX.1-1988 (ustar) format.
185
186
187.. data:: GNU_FORMAT
188
189 GNU tar format.
190
191
192.. data:: PAX_FORMAT
193
194 POSIX.1-2001 (pax) format.
195
196
197.. data:: DEFAULT_FORMAT
198
199 The default format for creating archives. This is currently :const:`GNU_FORMAT`.
200
201
202.. seealso::
203
204 Module :mod:`zipfile`
205 Documentation of the :mod:`zipfile` standard module.
206
207 `GNU tar manual, Basic Tar Format <http://www.gnu.org/software/tar/manual/html_node/tar_134.html#SEC134>`_
208 Documentation for tar archive files, including GNU tar extensions.
209
210.. % -----------------
211.. % TarFile Objects
212.. % -----------------
213
214
215.. _tarfile-objects:
216
217TarFile Objects
218---------------
219
220The :class:`TarFile` object provides an interface to a tar archive. A tar
221archive is a sequence of blocks. An archive member (a stored file) is made up of
222a header block followed by data blocks. It is possible to store a file in a tar
223archive several times. Each archive member is represented by a :class:`TarInfo`
224object, see :ref:`tarinfo-objects` for details.
225
226
227.. 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)
228
229 All following arguments are optional and can be accessed as instance attributes
230 as well.
231
232 *name* is the pathname of the archive. It can be omitted if *fileobj* is given.
233 In this case, the file object's :attr:`name` attribute is used if it exists.
234
235 *mode* is either ``'r'`` to read from an existing archive, ``'a'`` to append
236 data to an existing file or ``'w'`` to create a new file overwriting an existing
237 one.
238
239 If *fileobj* is given, it is used for reading or writing data. If it can be
240 determined, *mode* is overridden by *fileobj*'s mode. *fileobj* will be used
241 from position 0.
242
243 .. note::
244
245 *fileobj* is not closed, when :class:`TarFile` is closed.
246
247 *format* controls the archive format. It must be one of the constants
248 :const:`USTAR_FORMAT`, :const:`GNU_FORMAT` or :const:`PAX_FORMAT` that are
249 defined at module level.
250
251 .. versionadded:: 2.6
252
253 The *tarinfo* argument can be used to replace the default :class:`TarInfo` class
254 with a different one.
255
256 .. versionadded:: 2.6
257
258 If *dereference* is ``False``, add symbolic and hard links to the archive. If it
259 is ``True``, add the content of the target files to the archive. This has no
260 effect on systems that do not support symbolic links.
261
262 If *ignore_zeros* is ``False``, treat an empty block as the end of the archive.
263 If it is *True*, skip empty (and invalid) blocks and try to get as many members
264 as possible. This is only useful for reading concatenated or damaged archives.
265
266 *debug* can be set from ``0`` (no debug messages) up to ``3`` (all debug
267 messages). The messages are written to ``sys.stderr``.
268
269 If *errorlevel* is ``0``, all errors are ignored when using :meth:`extract`.
270 Nevertheless, they appear as error messages in the debug output, when debugging
271 is enabled. If ``1``, all *fatal* errors are raised as :exc:`OSError` or
272 :exc:`IOError` exceptions. If ``2``, all *non-fatal* errors are raised as
273 :exc:`TarError` exceptions as well.
274
275 The *encoding* and *errors* arguments control the way strings are converted to
276 unicode objects and vice versa. The default settings will work for most users.
277 See section :ref:`tar-unicode` for in-depth information.
278
279 .. versionadded:: 2.6
280
281 The *pax_headers* argument is an optional dictionary of unicode strings which
282 will be added as a pax global header if *format* is :const:`PAX_FORMAT`.
283
284 .. versionadded:: 2.6
285
286
287.. method:: TarFile.open(...)
288
289 Alternative constructor. The :func:`open` function on module level is actually a
290 shortcut to this classmethod. See section :ref:`tarfile-mod` for details.
291
292
293.. method:: TarFile.getmember(name)
294
295 Return a :class:`TarInfo` object for member *name*. If *name* can not be found
296 in the archive, :exc:`KeyError` is raised.
297
298 .. note::
299
300 If a member occurs more than once in the archive, its last occurrence is assumed
301 to be the most up-to-date version.
302
303
304.. method:: TarFile.getmembers()
305
306 Return the members of the archive as a list of :class:`TarInfo` objects. The
307 list has the same order as the members in the archive.
308
309
310.. method:: TarFile.getnames()
311
312 Return the members as a list of their names. It has the same order as the list
313 returned by :meth:`getmembers`.
314
315
316.. method:: TarFile.list(verbose=True)
317
318 Print a table of contents to ``sys.stdout``. If *verbose* is :const:`False`,
319 only the names of the members are printed. If it is :const:`True`, output
320 similar to that of :program:`ls -l` is produced.
321
322
323.. method:: TarFile.next()
324
325 Return the next member of the archive as a :class:`TarInfo` object, when
326 :class:`TarFile` is opened for reading. Return ``None`` if there is no more
327 available.
328
329
330.. method:: TarFile.extractall([path[, members]])
331
332 Extract all members from the archive to the current working directory or
333 directory *path*. If optional *members* is given, it must be a subset of the
334 list returned by :meth:`getmembers`. Directory information like owner,
335 modification time and permissions are set after all members have been extracted.
336 This is done to work around two problems: A directory's modification time is
337 reset each time a file is created in it. And, if a directory's permissions do
338 not allow writing, extracting files to it will fail.
339
Lars Gustäbel89241a32007-08-30 20:24:31 +0000340 .. warning::
341
342 Never extract archives from untrusted sources without prior inspection.
343 It is possible that files are created outside of *path*, e.g. members
344 that have absolute filenames starting with ``"/"`` or filenames with two
345 dots ``".."``.
346
Georg Brandl8ec7f652007-08-15 14:28:01 +0000347 .. versionadded:: 2.5
348
349
350.. method:: TarFile.extract(member[, path])
351
352 Extract a member from the archive to the current working directory, using its
353 full name. Its file information is extracted as accurately as possible. *member*
354 may be a filename or a :class:`TarInfo` object. You can specify a different
355 directory using *path*.
356
357 .. note::
358
359 Because the :meth:`extract` method allows random access to a tar archive there
360 are some issues you must take care of yourself. See the description for
361 :meth:`extractall` above.
362
Lars Gustäbel89241a32007-08-30 20:24:31 +0000363 .. warning::
364
365 See the warning for :meth:`extractall`.
366
Georg Brandl8ec7f652007-08-15 14:28:01 +0000367
368.. method:: TarFile.extractfile(member)
369
370 Extract a member from the archive as a file object. *member* may be a filename
371 or a :class:`TarInfo` object. If *member* is a regular file, a file-like object
372 is returned. If *member* is a link, a file-like object is constructed from the
373 link's target. If *member* is none of the above, ``None`` is returned.
374
375 .. note::
376
377 The file-like object is read-only and provides the following methods:
378 :meth:`read`, :meth:`readline`, :meth:`readlines`, :meth:`seek`, :meth:`tell`.
379
380
381.. method:: TarFile.add(name[, arcname[, recursive[, exclude]]])
382
383 Add the file *name* to the archive. *name* may be any type of file (directory,
384 fifo, symbolic link, etc.). If given, *arcname* specifies an alternative name
385 for the file in the archive. Directories are added recursively by default. This
386 can be avoided by setting *recursive* to :const:`False`. If *exclude* is given
387 it must be a function that takes one filename argument and returns a boolean
388 value. Depending on this value the respective file is either excluded
389 (:const:`True`) or added (:const:`False`).
390
391 .. versionchanged:: 2.6
392 Added the *exclude* parameter.
393
394
395.. method:: TarFile.addfile(tarinfo[, fileobj])
396
397 Add the :class:`TarInfo` object *tarinfo* to the archive. If *fileobj* is given,
398 ``tarinfo.size`` bytes are read from it and added to the archive. You can
399 create :class:`TarInfo` objects using :meth:`gettarinfo`.
400
401 .. note::
402
403 On Windows platforms, *fileobj* should always be opened with mode ``'rb'`` to
404 avoid irritation about the file size.
405
406
407.. method:: TarFile.gettarinfo([name[, arcname[, fileobj]]])
408
409 Create a :class:`TarInfo` object for either the file *name* or the file object
410 *fileobj* (using :func:`os.fstat` on its file descriptor). You can modify some
411 of the :class:`TarInfo`'s attributes before you add it using :meth:`addfile`.
412 If given, *arcname* specifies an alternative name for the file in the archive.
413
414
415.. method:: TarFile.close()
416
417 Close the :class:`TarFile`. In write mode, two finishing zero blocks are
418 appended to the archive.
419
420
421.. attribute:: TarFile.posix
422
423 Setting this to :const:`True` is equivalent to setting the :attr:`format`
424 attribute to :const:`USTAR_FORMAT`, :const:`False` is equivalent to
425 :const:`GNU_FORMAT`.
426
427 .. versionchanged:: 2.4
428 *posix* defaults to :const:`False`.
429
430 .. deprecated:: 2.6
431 Use the :attr:`format` attribute instead.
432
433
434.. attribute:: TarFile.pax_headers
435
436 A dictionary containing key-value pairs of pax global headers.
437
438 .. versionadded:: 2.6
439
440.. % -----------------
441.. % TarInfo Objects
442.. % -----------------
443
444
445.. _tarinfo-objects:
446
447TarInfo Objects
448---------------
449
450A :class:`TarInfo` object represents one member in a :class:`TarFile`. Aside
451from storing all required attributes of a file (like file type, size, time,
452permissions, owner etc.), it provides some useful methods to determine its type.
453It does *not* contain the file's data itself.
454
455:class:`TarInfo` objects are returned by :class:`TarFile`'s methods
456:meth:`getmember`, :meth:`getmembers` and :meth:`gettarinfo`.
457
458
459.. class:: TarInfo([name])
460
461 Create a :class:`TarInfo` object.
462
463
464.. method:: TarInfo.frombuf(buf)
465
466 Create and return a :class:`TarInfo` object from string buffer *buf*.
467
468 .. versionadded:: 2.6
469 Raises :exc:`HeaderError` if the buffer is invalid..
470
471
472.. method:: TarInfo.fromtarfile(tarfile)
473
474 Read the next member from the :class:`TarFile` object *tarfile* and return it as
475 a :class:`TarInfo` object.
476
477 .. versionadded:: 2.6
478
479
480.. method:: TarInfo.tobuf([format[, encoding [, errors]]])
481
482 Create a string buffer from a :class:`TarInfo` object. For information on the
483 arguments see the constructor of the :class:`TarFile` class.
484
485 .. versionchanged:: 2.6
486 The arguments were added.
487
488A ``TarInfo`` object has the following public data attributes:
489
490
491.. attribute:: TarInfo.name
492
493 Name of the archive member.
494
495
496.. attribute:: TarInfo.size
497
498 Size in bytes.
499
500
501.. attribute:: TarInfo.mtime
502
503 Time of last modification.
504
505
506.. attribute:: TarInfo.mode
507
508 Permission bits.
509
510
511.. attribute:: TarInfo.type
512
513 File type. *type* is usually one of these constants: :const:`REGTYPE`,
514 :const:`AREGTYPE`, :const:`LNKTYPE`, :const:`SYMTYPE`, :const:`DIRTYPE`,
515 :const:`FIFOTYPE`, :const:`CONTTYPE`, :const:`CHRTYPE`, :const:`BLKTYPE`,
516 :const:`GNUTYPE_SPARSE`. To determine the type of a :class:`TarInfo` object
517 more conveniently, use the ``is_*()`` methods below.
518
519
520.. attribute:: TarInfo.linkname
521
522 Name of the target file name, which is only present in :class:`TarInfo` objects
523 of type :const:`LNKTYPE` and :const:`SYMTYPE`.
524
525
526.. attribute:: TarInfo.uid
527
528 User ID of the user who originally stored this member.
529
530
531.. attribute:: TarInfo.gid
532
533 Group ID of the user who originally stored this member.
534
535
536.. attribute:: TarInfo.uname
537
538 User name.
539
540
541.. attribute:: TarInfo.gname
542
543 Group name.
544
545
546.. attribute:: TarInfo.pax_headers
547
548 A dictionary containing key-value pairs of an associated pax extended header.
549
550 .. versionadded:: 2.6
551
552A :class:`TarInfo` object also provides some convenient query methods:
553
554
555.. method:: TarInfo.isfile()
556
557 Return :const:`True` if the :class:`Tarinfo` object is a regular file.
558
559
560.. method:: TarInfo.isreg()
561
562 Same as :meth:`isfile`.
563
564
565.. method:: TarInfo.isdir()
566
567 Return :const:`True` if it is a directory.
568
569
570.. method:: TarInfo.issym()
571
572 Return :const:`True` if it is a symbolic link.
573
574
575.. method:: TarInfo.islnk()
576
577 Return :const:`True` if it is a hard link.
578
579
580.. method:: TarInfo.ischr()
581
582 Return :const:`True` if it is a character device.
583
584
585.. method:: TarInfo.isblk()
586
587 Return :const:`True` if it is a block device.
588
589
590.. method:: TarInfo.isfifo()
591
592 Return :const:`True` if it is a FIFO.
593
594
595.. method:: TarInfo.isdev()
596
597 Return :const:`True` if it is one of character device, block device or FIFO.
598
599.. % ------------------------
600.. % Examples
601.. % ------------------------
602
603
604.. _tar-examples:
605
606Examples
607--------
608
609How to extract an entire tar archive to the current working directory::
610
611 import tarfile
612 tar = tarfile.open("sample.tar.gz")
613 tar.extractall()
614 tar.close()
615
616How to create an uncompressed tar archive from a list of filenames::
617
618 import tarfile
619 tar = tarfile.open("sample.tar", "w")
620 for name in ["foo", "bar", "quux"]:
621 tar.add(name)
622 tar.close()
623
624How to read a gzip compressed tar archive and display some member information::
625
626 import tarfile
627 tar = tarfile.open("sample.tar.gz", "r:gz")
628 for tarinfo in tar:
629 print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
630 if tarinfo.isreg():
631 print "a regular file."
632 elif tarinfo.isdir():
633 print "a directory."
634 else:
635 print "something else."
636 tar.close()
637
638How to create a tar archive with faked information::
639
640 import tarfile
641 tar = tarfile.open("sample.tar.gz", "w:gz")
642 for name in namelist:
643 tarinfo = tar.gettarinfo(name, "fakeproj-1.0/" + name)
644 tarinfo.uid = 123
645 tarinfo.gid = 456
646 tarinfo.uname = "johndoe"
647 tarinfo.gname = "fake"
648 tar.addfile(tarinfo, file(name))
649 tar.close()
650
651The *only* way to extract an uncompressed tar stream from ``sys.stdin``::
652
653 import sys
654 import tarfile
655 tar = tarfile.open(mode="r|", fileobj=sys.stdin)
656 for tarinfo in tar:
657 tar.extract(tarinfo)
658 tar.close()
659
660.. % ------------
661.. % Tar format
662.. % ------------
663
664
665.. _tar-formats:
666
667Supported tar formats
668---------------------
669
670There are three tar formats that can be created with the :mod:`tarfile` module:
671
672* The POSIX.1-1988 ustar format (:const:`USTAR_FORMAT`). It supports filenames
673 up to a length of at best 256 characters and linknames up to 100 characters. The
674 maximum file size is 8 gigabytes. This is an old and limited but widely
675 supported format.
676
677* The GNU tar format (:const:`GNU_FORMAT`). It supports long filenames and
678 linknames, files bigger than 8 gigabytes and sparse files. It is the de facto
679 standard on GNU/Linux systems. :mod:`tarfile` fully supports the GNU tar
680 extensions for long names, sparse file support is read-only.
681
682* The POSIX.1-2001 pax format (:const:`PAX_FORMAT`). It is the most flexible
683 format with virtually no limits. It supports long filenames and linknames, large
684 files and stores pathnames in a portable way. However, not all tar
685 implementations today are able to handle pax archives properly.
686
687 The *pax* format is an extension to the existing *ustar* format. It uses extra
688 headers for information that cannot be stored otherwise. There are two flavours
689 of pax headers: Extended headers only affect the subsequent file header, global
690 headers are valid for the complete archive and affect all following files. All
691 the data in a pax header is encoded in *UTF-8* for portability reasons.
692
693There are some more variants of the tar format which can be read, but not
694created:
695
696* The ancient V7 format. This is the first tar format from Unix Seventh Edition,
697 storing only regular files and directories. Names must not be longer than 100
698 characters, there is no user/group name information. Some archives have
699 miscalculated header checksums in case of fields with non-ASCII characters.
700
701* The SunOS tar extended format. This format is a variant of the POSIX.1-2001
702 pax format, but is not compatible.
703
704.. % ----------------
705.. % Unicode issues
706.. % ----------------
707
708
709.. _tar-unicode:
710
711Unicode issues
712--------------
713
714The tar format was originally conceived to make backups on tape drives with the
715main focus on preserving file system information. Nowadays tar archives are
716commonly used for file distribution and exchanging archives over networks. One
717problem of the original format (that all other formats are merely variants of)
718is that there is no concept of supporting different character encodings. For
719example, an ordinary tar archive created on a *UTF-8* system cannot be read
720correctly on a *Latin-1* system if it contains non-ASCII characters. Names (i.e.
721filenames, linknames, user/group names) containing these characters will appear
722damaged. Unfortunately, there is no way to autodetect the encoding of an
723archive.
724
725The pax format was designed to solve this problem. It stores non-ASCII names
726using the universal character encoding *UTF-8*. When a pax archive is read,
727these *UTF-8* names are converted to the encoding of the local file system.
728
729The details of unicode conversion are controlled by the *encoding* and *errors*
730keyword arguments of the :class:`TarFile` class.
731
732The default value for *encoding* is the local character encoding. It is deduced
733from :func:`sys.getfilesystemencoding` and :func:`sys.getdefaultencoding`. In
734read mode, *encoding* is used exclusively to convert unicode names from a pax
735archive to strings in the local character encoding. In write mode, the use of
736*encoding* depends on the chosen archive format. In case of :const:`PAX_FORMAT`,
737input names that contain non-ASCII characters need to be decoded before being
738stored as *UTF-8* strings. The other formats do not make use of *encoding*
739unless unicode objects are used as input names. These are converted to 8-bit
740character strings before they are added to the archive.
741
742The *errors* argument defines how characters are treated that cannot be
743converted to or from *encoding*. Possible values are listed in section
744:ref:`codec-base-classes`. In read mode, there is an additional scheme
745``'utf-8'`` which means that bad characters are replaced by their *UTF-8*
746representation. This is the default scheme. In write mode the default value for
747*errors* is ``'strict'`` to ensure that name information is not altered
748unnoticed.
749