Open a tar archive for reading, writing or appending. Return an appropriate TarFile class. mode: 'r' or 'r:*' open for reading with transparent compression 'r:' open for reading exclusively uncompressed 'r:gz' open for readi
(cls, name=None, mode="r", fileobj=None, bufsize=RECORDSIZE, **kwargs)
| 1754 | |
| 1755 | @classmethod |
| 1756 | def open(cls, name=None, mode="r", fileobj=None, bufsize=RECORDSIZE, **kwargs): |
| 1757 | """Open a tar archive for reading, writing or appending. Return |
| 1758 | an appropriate TarFile class. |
| 1759 | |
| 1760 | mode: |
| 1761 | 'r' or 'r:*' open for reading with transparent compression |
| 1762 | 'r:' open for reading exclusively uncompressed |
| 1763 | 'r:gz' open for reading with gzip compression |
| 1764 | 'r:bz2' open for reading with bzip2 compression |
| 1765 | 'r:xz' open for reading with lzma compression |
| 1766 | 'a' or 'a:' open for appending, creating the file if necessary |
| 1767 | 'w' or 'w:' open for writing without compression |
| 1768 | 'w:gz' open for writing with gzip compression |
| 1769 | 'w:bz2' open for writing with bzip2 compression |
| 1770 | 'w:xz' open for writing with lzma compression |
| 1771 | |
| 1772 | 'x' or 'x:' create a tarfile exclusively without compression, raise |
| 1773 | an exception if the file is already created |
| 1774 | 'x:gz' create a gzip compressed tarfile, raise an exception |
| 1775 | if the file is already created |
| 1776 | 'x:bz2' create a bzip2 compressed tarfile, raise an exception |
| 1777 | if the file is already created |
| 1778 | 'x:xz' create an lzma compressed tarfile, raise an exception |
| 1779 | if the file is already created |
| 1780 | |
| 1781 | 'r|*' open a stream of tar blocks with transparent compression |
| 1782 | 'r|' open an uncompressed stream of tar blocks for reading |
| 1783 | 'r|gz' open a gzip compressed stream of tar blocks |
| 1784 | 'r|bz2' open a bzip2 compressed stream of tar blocks |
| 1785 | 'r|xz' open an lzma compressed stream of tar blocks |
| 1786 | 'w|' open an uncompressed stream for writing |
| 1787 | 'w|gz' open a gzip compressed stream for writing |
| 1788 | 'w|bz2' open a bzip2 compressed stream for writing |
| 1789 | 'w|xz' open an lzma compressed stream for writing |
| 1790 | """ |
| 1791 | |
| 1792 | if not name and not fileobj: |
| 1793 | raise ValueError("nothing to open") |
| 1794 | |
| 1795 | if mode in ("r", "r:*"): |
| 1796 | # Find out which *open() is appropriate for opening the file. |
| 1797 | def not_compressed(comptype): |
| 1798 | return cls.OPEN_METH[comptype] == 'taropen' |
| 1799 | error_msgs = [] |
| 1800 | for comptype in sorted(cls.OPEN_METH, key=not_compressed): |
| 1801 | func = getattr(cls, cls.OPEN_METH[comptype]) |
| 1802 | if fileobj is not None: |
| 1803 | saved_pos = fileobj.tell() |
| 1804 | try: |
| 1805 | return func(name, "r", fileobj, **kwargs) |
| 1806 | except (ReadError, CompressionError) as e: |
| 1807 | error_msgs.append(f'- method {comptype}: {e!r}') |
| 1808 | if fileobj is not None: |
| 1809 | fileobj.seek(saved_pos) |
| 1810 | continue |
| 1811 | error_msgs_summary = '\n'.join(error_msgs) |
| 1812 | raise ReadError(f"file could not be opened successfully:\n{error_msgs_summary}") |
| 1813 |